rbvinc Posted May 7, 2012 Share Posted May 7, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/ Share on other sites More sharing options...
smerny Posted May 7, 2012 Share Posted May 7, 2012 Below is my code. where? Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343674 Share on other sites More sharing options...
rbvinc Posted May 7, 2012 Author Share Posted May 7, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343675 Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 What errors do you get? Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343701 Share on other sites More sharing options...
rbvinc Posted May 7, 2012 Author Share Posted May 7, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343705 Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 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; } Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343723 Share on other sites More sharing options...
rbvinc Posted May 7, 2012 Author Share Posted May 7, 2012 I am lost. Can give me code for that looping please. Thank you, table = customers fields = country, companyname Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343731 Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343732 Share on other sites More sharing options...
rbvinc Posted May 7, 2012 Author Share Posted May 7, 2012 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, Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343744 Share on other sites More sharing options...
Jessica Posted May 7, 2012 Share Posted May 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343764 Share on other sites More sharing options...
Erdy Posted May 8, 2012 Share Posted May 8, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1343877 Share on other sites More sharing options...
rbvinc Posted May 9, 2012 Author Share Posted May 9, 2012 Erdy, I tried. No luck. Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1344182 Share on other sites More sharing options...
rbvinc Posted May 9, 2012 Author Share Posted May 9, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1344245 Share on other sites More sharing options...
Jessica Posted May 9, 2012 Share Posted May 9, 2012 So, you didn't try the solution that was suggested. Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1344267 Share on other sites More sharing options...
rbvinc Posted May 10, 2012 Author Share Posted May 10, 2012 Jesi, I asked for help.... if you can help me out. Once I cross then I am on my own... Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1344439 Share on other sites More sharing options...
rbvinc Posted May 11, 2012 Author Share Posted May 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1344892 Share on other sites More sharing options...
rbvinc Posted May 12, 2012 Author Share Posted May 12, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262199-php-mysql/#findComment-1344944 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.