flyboeing Posted October 22, 2013 Share Posted October 22, 2013 Hello all, I have a small problem with my code. I have a table with the information regarding the fleet of aircrafts an airline has and if an airline has some aircraft on order. This table consist out of 3 columns; airline_id, aircraft_id and besteld (ordered). With my code I only get the aircraft that are in use (so where the ordered column has a value of 0). This working of me, so when an airline has no aircraft ordered, non in shown. Only the heading of the table stays visible, but I would like to see that it ain't visible any more. Where do I place my heading so it won't be visible when there are no orders? Sorry for my crappy php-coding <?php $con = mysql_connect("localhost","xxxx","xxxx"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("deb7560n3_ai", $con); echo "<div id='huidigevloot'>Huidige vloot</div>"; // Construct our join query $query1 = " SELECT DISTINCT Airline_Aircraft.airline_id, Airlines.airline_id, Airlines.name, Airline_Aircraft.besteld, Airline_Aircraft.aircraft_id, Aircraft_id1, Aircraft.name1, d7ul5_categories.id, d7ul5_categories.path, d7ul5_content.id, d7ul5_content.title, d7ul5_content.alias, d7ul5_content.state, d7ul5_content.catid "." FROM Airline_Aircraft"." LEFT JOIN Aircraft "." ON Airline_Aircraft.aircraft_id = Aircraft.aircraft_id1 LEFT JOIN Airlines "." ON Airline_Aircraft.airline_id = Airlines.airline_id LEFT JOIN d7ul5_content "." ON Aircraft.name1 = d7ul5_content.title LEFT JOIN d7ul5_categories "." ON d7ul5_content.catid = d7ul5_categories.id WHERE "." Airlines.name = '$title_artikel' AND d7ul5_content.state = 1 ORDER BY Aircraft.name1 ASC" ; $result1 = mysql_query($query1) or die(mysql_error()); $num_rows1 = mysql_num_rows($result1); $x = 0; $num_cols = 4; echo " <table cellpadding='0' cellspacing='0' border='0' width='100%'> <tr> <td valign='top' width='25%'>"; while($row = mysql_fetch_array($result1)) { if ($row['name1'] == $row['title'] && $row['besteld'] == '0') { echo "<a href='../".$row['path']."/".$row['id']."-".$row['alias']."'>".$row['name1']."</a><p />"; } else { echo ""; } $x++; if ($x == ceil($num_rows1 / $num_cols)) { echo " </td> <td valign='top' width='25%'>"; $x = 0; } } echo " </td> </tr> </table>"; } ?> I hope someone can help me with my problem! Regards, Ruud Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 22, 2013 Share Posted October 22, 2013 (edited) Check that the query returned an results, if there are results display table, else display message if(($num_rows1 = mysql_num_rows($result1)) > 0) { $x = 0; $num_cols = 4; echo " <table cellpadding='0' cellspacing='0' border='0' width='100%'> <tr> <td valign='top' width='25%'>"; while($row = mysql_fetch_array($result1)) { if ($row['name1'] == $row['title'] && $row['besteld'] == '0') { echo "<a href='../".$row['path']."/".$row['id']."-".$row['alias']."'>".$row['name1']."</a><p />"; } else { echo ""; } $x++; if ($x == ceil($num_rows1 / $num_cols)) { echo " </td> <td valign='top' width='25%'>"; $x = 0; } } echo " </td> </tr> </table>"; } else { // display no planes found message } Edited October 22, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
flyboeing Posted October 22, 2013 Author Share Posted October 22, 2013 Thanks Ch0cu3r, that part was missing. I also needed to change my WHERE-query, I added the search criteria that it only needs to look for 'besteld' = 0. Quote Link to comment Share on other sites More sharing options...
flyboeing Posted January 13, 2014 Author Share Posted January 13, 2014 After making some changes the code above is working perfectly. Now all aircrafts which an airline owns are displayed in 4 columns. The aircraft is a link to the article. With this script only the aircrafts are shown when the article exists. If the article does not exist, the aircraft is not visible. This is also the part that I want to change. If an article does not exist, I would like to see the aircraft name (without the link). Can someone help me with this? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 13, 2014 Share Posted January 13, 2014 (edited) Move the WHERE conditions to the JOIN ON conditions for their respective table. WHERE on LEFT JOINed tables results in INNER JOIN behaviour $query1 = "SELECT DISTINCT Airline_Aircraft.airline_id , Airlines.airline_id , Airlines.name , Airline_Aircraft.besteld , Airline_Aircraft.aircraft_id , Aircraft_id1 , Aircraft.name1 , d7ul5_categories.id , d7ul5_categories.path , d7ul5_content.id , d7ul5_content.title , d7ul5_content.alias , d7ul5_content.state , d7ul5_content.catid FROM Airline_Aircraft LEFT JOIN Aircraft ON Airline_Aircraft.aircraft_id = Aircraft.aircraft_id1 LEFT JOIN Airlines ON Airline_Aircraft.airline_id = Airlines.airline_id AND Airlines.name = '$title_artikel' LEFT JOIN d7ul5_content ON Aircraft.name1 = d7ul5_content.title AND d7ul5_content.state = 1 LEFT JOIN d7ul5_categories ON d7ul5_content.catid = d7ul5_categories.id ORDER BY Aircraft.name1 ASC" ; Are you sure you need all LEFT JOINS? They are much slower than INNER JOINS and only required when there may not be a matching row in the joined table Edited January 13, 2014 by Barand Quote Link to comment 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.