Jump to content

[SOLVED] adding condition to echo statement


fingirl

Recommended Posts

Hi there,

 

I'm successfully dispaying some database content in a table with this code:

 

an echo statement here...

<td valign = 'top'>

<a href = '".$row[demo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Demo  </a>(mp3)   <a href = '".$row[characterdemo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Character Demo  </a>(mp3) <a href = '".$row[animation]."' onclick=\"window.open(this.href, 'popupwindow', 'width=800, height=600, left=150, top=100, scrollbars, resizable'); return false;\">Listen to Animation Demo  </a>(mp3)

</td>

 

Now I want to add a condition that the <a href ... is only displayed if the field it calls is not empty.

 

I'm getting very confused about adding in the php to the rather lengthy snippet above. My code opens <?php right at the beginning and closes ?>right at the end.

 

Here's where I've got to so far - but I am getting a php syntax error. Missing a comma or semi colon somewhere.

 

an echo statement here...

<td valign = 'top'>" if(!empty($row[demo])) {echo "<a href = '".$row[demo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Demo  </a>(mp3)  "}; echo "<a href = '".$row[characterdemo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Character Demo  </a>(mp3) <a href = '".$row[animation]."' onclick=\"window.open(this.href, 'popupwindow', 'width=800, height=600, left=150, top=100, scrollbars, resizable'); return false;\">Listen to Animation Demo  </a>(mp3)

</td>

 

Any assistance greatly appreciated.

 

Fin

Link to comment
Share on other sites

OK. Thanks. I've commented in code where my problem is, like so <!-- NEED TO FIX HERE -->

 



<?php
$showalltalent=$_POST['showalltalent'];
$showallfemaletalent=$_POST['showallfemaletalent'];
$showallmaletalent=$_POST['showallmaletalent'];

//when working locally
@ $db = mysql_pconnect('localhost', 'troy', 'troy1');
//when working on bluehost
//@ $db = mysql_pconnect('localhost', 'finfacto_troy', 'troy1');

if(!$db)
{
	echo 'Sorry, could not connect to database. Please try later or contact database administrator.';
	exit;
}

//when working locally
mysql_select_db('jermyntalent');
//when working on bluehost
//mysql_select_db('finfacto_jermyntalent');

/* SHOW ALL RECORDS -----------------------------------------------------------------------------*/
// variables for a search to show all records
$querytoshowall = "select id, firstname, lastname, shortbiog, image, demo, characterdemo, animation from talent order by firstname";
$showallresult = mysql_query($querytoshowall);
$number = mysql_num_rows($showallresult);

// script triggered by clicking the Show All button
if(isset($showalltalent))
{
	echo "<h3>You requested to view all records</h3>";

	while($row = mysql_fetch_array($showallresult))
	{
		echo "<table width = '90%' border = '0' cellpadding='3'>
				<tr>
				    <td valign = 'top' rowspan='4' width='56'><img src='".$row[image]."' width='110' alt='image of ".$row[firstname]. " ". stripslashes($row[lastname]). "' /></td>
					<td valign = 'top'><h3>".$row[firstname] . " " . stripslashes($row[lastname]). "</h3></td>
				</tr>";
		echo "<tr>
				<td valign = 'top' >".stripslashes($row[shortbiog])."</td>
			 </tr>
			 <tr><!-- NEED TO FIX HERE --><td valign = 'top'>" if(!empty($row[demo])) {echo "<a href = '".$row[demo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Demo  </a>(mp3)  "}; echo "<a href = '".$row[characterdemo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Character Demo  </a>(mp3) <a href = '".$row[animation]."' onclick=\"window.open(this.href, 'popupwindow', 'width=800, height=600, left=150, top=100, scrollbars, resizable'); return false;\">Listen to Animation Demo  </a>(mp3)</td>
			</tr>
			<tr>
				<td valign = 'top'><a href='details.php?id=$row[id]' onclick=\"window.open(this.href, 'popupwindow', 'width=550, height=500, left=250, top=150, scrollbars, resizable'); return false;\">More details</a>   <a href = 'emailenquiry.php?id=$row[id]' onclick=\"window.open(this.href, 'popupwindow', 'width=650, height=500, left=200, top=150, scrollbars, resizable'); return false;\"'>Email Enquiry</a></td>
			</tr>
			</table>";
		echo "<hr />";
	}

	if($number < 1)
	{
		echo "<h3>There were no results for your search</h3>";
	}

	mysql_free_result($showallresult);
}

/* SHOW ALL FEMALE RECORDS -----------------------------------------------------------------------*/
//variables for a search to find females
$queryallfemales = "select id, firstname, lastname, shortbiog, image, demo, characterdemo, animation from talent where gender='female' order by firstname";
$showallfemalesresult = mysql_query($queryallfemales);
$numberfemales = mysql_num_rows($showallfemalesresult);

// script triggered by clicking the Show All Females button
if(isset($showallfemaletalent))
{
	echo "<h3>You requested to view all records of females</h3>";

	while($row = mysql_fetch_array($showallfemalesresult))
	{
		echo "<table width = '90%' border = '0' cellpadding='3'>
				<tr>
				    <td valign = 'top' rowspan='4' width='56'><img src='".$row[image]."' width='110' alt='image of ".$row[firstname]. " ". stripslashes($row[lastname]). "' /></td>
					<td valign = 'top'><h3>".$row[firstname] . " " . stripslashes($row[lastname]). "</h3></td>
				</tr>";
		echo "<tr>
				<td valign = 'top' >".stripslashes($row[shortbiog])."</td>
			 </tr>
			 <tr>
				<td valign = 'top'><a href = '".$row[demo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Demo  </a>(mp3)   <a href = '".$row[characterdemo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Character Demo  </a>(mp3) <a href = '".$row[animation]."' onclick=\"window.open(this.href, 'popupwindow', 'width=800, height=600, left=150, top=100, scrollbars, resizable'); return false;\">Listen to Animation Demo  </a>(mp3)</td>
			</tr>
			<tr>
				<td valign = 'top'><a href='details.php?id=$row[id]' onclick=\"window.open(this.href, 'popupwindow', 'width=550, height=500, left=250, top=150, scrollbars, resizable'); return false;\">More details</a>   <a href = 'emailenquiry.php?id=$row[id]' onclick=\"window.open(this.href, 'popupwindow', 'width=650, height=500, left=200, top=150, scrollbars, resizable'); return false;\"'>Email Enquiry</a></td>
			</tr>
			</table>";
		echo "<hr />";
	}

	if($numberfemales < 1)
	{
		echo "<h3>There were no results for your search</h3>";
	}

	mysql_free_result($showallfemalesresult);
}

/* SHOW ALL MALE RECORDS -------------------------------------------------------------------------*/
//variables for a search to find males
$queryallmales = "select id, firstname, lastname, shortbiog, image, demo, characterdemo, animation from talent where gender='male' order by firstname";
$showallmalesresult = mysql_query($queryallmales);
$numbermales = mysql_num_rows($showallmalesresult);

// script triggered by clicking the Show All Males button
if(isset($showallmaletalent))
{
	echo "<h3>You requested to view all records of males</h3>";

	while($row = mysql_fetch_array($showallmalesresult))
	{
		echo "<table width = '90%' border = '0' cellpadding='3'>
				<tr>
				    <td valign = 'top' rowspan='4' width='56'><img src='".$row[image]."' width='110' alt='image of ".$row[firstname]. " ". stripslashes($row[lastname]). "' /></td>
					<td valign = 'top'><h3>".$row[firstname] . " " . stripslashes($row[lastname]). "</h3></td>
				</tr>";
		echo "<tr>
				<td valign = 'top' >".stripslashes($row[shortbiog])."</td>
			 </tr>
			 <tr>
				<td valign = 'top'><a href = '".$row[demo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Demo  </a>(mp3)   <a href = '".$row[characterdemo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Character Demo  </a>(mp3) <a href = '".$row[animation]."' onclick=\"window.open(this.href, 'popupwindow', 'width=800, height=600, left=150, top=100, scrollbars, resizable'); return false;\">Listen to Animation Demo  </a>(mp3)</td>
			</tr>
			<tr>
				<td valign = 'top'><a href='details.php?id=$row[id]' onclick=\"window.open(this.href, 'popupwindow', 'width=550, height=500, left=250, top=150, scrollbars, resizable'); return false;\">More details</a>   <a href = 'emailenquiry.php?id=$row[id]' onclick=\"window.open(this.href, 'popupwindow', 'width=650, height=500, left=200, top=150, scrollbars, resizable'); return false;\"'>Email Enquiry</a></td>
			</tr>
			</table>";
		echo "<hr />";
	}

	if($numbermales < 1)
	{
		echo "<h3>There were no results for your search</h3>";
	}

	mysql_free_result($showallmalesresult);
}


?>

Link to comment
Share on other sites

				 <tr><!-- NEED TO FIX HERE --><td valign = 'top'>" if(!empty($row[demo])) {echo "<a href = '".$row[demo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Demo  </a>(mp3)  "}; 

 

That should be

 

				 <tr><!-- NEED TO FIX HERE --><td valign = 'top'>" if(!empty($row[demo])) {echo "<a href = '".$row[demo]."' onclick=\"window.open(this.href, 'popupwindow', 'width=200, height=100, left=250, top=150, scrollbars, resizable'); return false;\">Listen to Demo  </a>(mp3) &nbsp";} 

 

Semi colon after " and before }, you've put it inside the " by accident, look closely.  No semi colon is required after }.

Link to comment
Share on other sites

Ah!

 

		echo "<tr>
				<td valign = 'top' >".stripslashes($row[shortbiog])."</td>
			 </tr>
			 <tr><!-- NEED TO FIX HERE --><td valign = 'top'>" if(!empty(

 

needs to be

 

		echo "<tr>
				<td valign = 'top' >".stripslashes($row[shortbiog])."</td>
			 </tr>
			 <tr><!-- NEED TO FIX HERE --><td valign = 'top'>"; if(!empty(

 

You didn't end the echo statement before you started the if.

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