Jump to content

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

I have about 100 records, but my loop is looping so many times, returning

1000+ repeated records. I need something like this. Help me out please with new code. Thanks.

 

 

Canada

. companyone

. companytwo

. companythree

 

USA

. xyz company

. zy company

. xz comapy

 

so on...

 

 

Link to comment
https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343705
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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.