Jump to content

Archived

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

rbvinc

PHP, MySql

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

 

 

Share this post


Link to post
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);

?>

Share this post


Link to post
Share on other sites

What errors do you get?

Share this post


Link to post
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...

 

 

Share this post


Link to post
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;
}

Share this post


Link to post
Share on other sites

I am lost. Can give me code for that looping please.

Thank you,

 

table = customers

fields = country, companyname

 

 

Share this post


Link to post
Share on other sites

No, you already know how to loop through results, unless you're just copying and pasting code without understanding it, in which case me giving you more to c&p won't teach you anything.

Share this post


Link to post
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,

 

Share this post


Link to post
Share on other sites

In your previous code you are looping through db results. Do the same thing. If you can't, go read your code and figure out what it does.

Share this post


Link to post
Share on other sites

Try to modify your code after this line:

 

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

 

if ($result1){

 

    // print here the table of customers

 

}

 

8)

Share this post


Link to post
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);

?>

 

 

Share this post


Link to post
Share on other sites

So, you didn't try the solution that was suggested.

Share this post


Link to post
Share on other sites

Jesi, I asked for help.... if you can help me out.

Once I cross then I am on my own...

Thanks,

 

Share this post


Link to post
Share on other sites

Jesirose, if you can help me fixing my code, it will be a life time help. I tried, could not figure it out please.

Thank you in advance.

Share this post


Link to post
Share on other sites

Jesirose, if can, please modify my code, make work,

it will be a lifetime Thank you, I tried could not follow.

Once I cross this, then I am on my own please.

I will be help to other people.

 

Thank you in advance.

Share this post


Link to post
Share on other sites

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