Jump to content

PHP, MySql


rbvinc

Recommended Posts

I am new, learing PHP. What I am trying to do is, first I want to put country,

under each counrty I want to put companynames. Below is my code. Can some one fixit it please for me. Thank you. Example below,

 

Canada

. companyone

. companytwo

. companythree

 

USA

. xyz company

. zy company

. xz comapy

 

so on...

 

 

Link to comment
https://forums.phpfreaks.com/topic/262199-php-mysql/
Share on other sites

Sorry...

************

 

 

<?php

$con = mysql_connect("localhost","root","test");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("northwind", $con);

 

$result = mysql_query("SELECT Country FROM customers order by country");

 

echo "<table border='1'>

<tr>

<th>Country</th>

</tr>";

 

while($row = mysql_fetch_array($result))

  {

echo "<tr>";

echo "<td>" . $row['Country'] . " </td>";

 

 

 

$result1 = mysql_query("SELECT * FROM customers where country = " . $row['Country'] . " order by CompanyName");

 

echo "<table border='1'>

<tr>

 

<th>Company Name</th>

<th>Country</th>

</tr>";

 

while($row = mysql_fetch_array($result1))

  {

echo "<tr>";

 

echo "<td>" . $row['CompanyName'] . " </td>";

echo "<td>" . $row['Country'] . " </td>";

echo "</tr>";

  }

echo "</table>";

 

}

echo "</tr>"; 

echo "</table>";

 

mysql_close($con);

?>

Link to comment
https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343675
Share on other sites

First problem is you shouldn't do a select inside a loop. Simple select the customers order by country name, then loop through them. Store the name of the country and when you get to a different one, print it out, otherwise don't print it.

<?php
$sql = "SELECT * FROM customers ORDER BY country";
$last_country = NULL;
while(){ //loop here
if($country != $last_country){
   $last_country = $country;
  echo $country;
}
echo $customer_info_here;
}

Link to comment
https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343723
Share on other sites

It is not copy and paste, I am so good with ASP, I copied ASP code, modified into PHP, to do the same process, and besides learning PHP, since got stuck I came here for help. If can please do help. This is not commercial. I am usinging northwind.mdb sample database for learning.

 

Thank you,

 

Link to comment
https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343744
Share on other sites

Erdy, I know it is wrong, but this what I have.....

 

Output to be,

 

Canada

. companyone

. companytwo

. companythree

 

USA

. xyz company

. zy company

. xz comapy

 

so on...

 

****************************** *************

<?php

$con = mysql_connect("localhost","root","test");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("northwind", $con);

 

$result = mysql_query("SELECT distinct country FROM customers order by country");

 

echo "<table border='1'>

<tr>

<th>Country</th>

</tr>";

 

while($row = mysql_fetch_array($result))

 

  {

echo "<tr bgcolor='#808080'>";

echo "<td>" . $row['country'] . " </td>";

 

echo "</tr>"; 

echo "</table>";

 

$result1 = mysql_query("SELECT * FROM customers where country = " . $row['country'] . " order by country");

 

echo "<table border='1'>

<tr>

 

<th colspan='2'>Company Name</th>

 

</tr>";

 

 

  echo "<tr>";

if ($result1)

  {

 

 

echo "<td>" . $row['Region'] . " </td>";

echo "<td>" . $row['country'] . " </td>";

 

  }

echo "</tr>"; 

echo "</table>";

 

}

 

 

mysql_close($con);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1344245
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.