Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/283190-if-else-statement-where-the-heading/
Share on other sites

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 by Ch0cu3r
  • 2 months later...

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?

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