Jump to content

[SOLVED] Nested ifs


harkly

Recommended Posts

am running these 'ifs' and at the end of them I want to have them print a line when no match was found.

 

I was thinking I could add something like this

 

if $results=null
then echo "Sorry no match was found"

 

but not sure if I can do that - pretty new to coding and not finding anything online that relates to this.

 

Someone suggested using a case statement but I don't think that would work.

 

Code so far::

$field=$_POST["field"];

$search=trim($_POST["find"]);


if ($field == "artist")

{

	echo "<b>Names of Artist(s)</b> <br><br>";

	$result = mysql_query("SELECT * FROM artist WHERE fullName LIKE '%$search%'");


	$count = count(0);

	while ($r=mysql_fetch_array($result))

	{	

	   $fullName=$r["fullName"];

		echo $count++; echo ". ".$fullName." <br>";

	}}


elseif ($field == "artwork")

{
	echo "<b>Titles</b> <br><br>";	

	$result = mysql_query("SELECT image.imgid, image.title, image.artid, image.dt, artist.artid, artist.fullName FROM image LEFT JOIN artist ON artist.artid = image.artid WHERE image.title LIKE '%$search%' ORDER BY title") or die(mysql_error());


	$count = count(0);

	while ($r=mysql_fetch_array($result))

	{	


	   $title=$r["title"];

	   $imgid=$r["imgid"];

	   $dt=$r["dt"];

	   $artid=$r["artid"];

	   $fullName=$r["fullName"];


		echo $count++; 	
		echo ". <a href='image.php?imgid=$r[imgid]'><img src=\"image/$imgid\" border=0 ></a>, <i>".$title."</i>, ".$dt." by <a href='artist.php?artid=$r[artid]'>".$fullName."</a><br>";

	}}


elseif ($field == "keywords")
{
echo "I will check all keywords.";
}
else
{
echo "I will search everywhere.";
}

Link to comment
Share on other sites

what about adding a nested if/else

 

I tried this but am getting an error

 

Parse error: syntax error, unexpected T_ELSE in /home/content/a/r/t/art2294/html/ifElse.php on line 63

if ($field == "artist")

{


	echo "<b>Names of Artist(s)</b> <br><br>";

	$result = mysql_query("SELECT * FROM artist WHERE fullName LIKE '%$search%'");

		if ($result != "0")
			{
				$count = count(0);


				//grab all the content


				while ($r=mysql_fetch_array($result))

				{	

				   //the format is $variable = $r["nameofmysqlcolumn"];

				   //modify these to match your mysql table columns


				   $fullName=$r["fullName"];

  

				   //display the row

				echo $count++; echo ". ".$fullName." <br>";

				}
		else
			{
				echo "Nothing found.";
			}
}


}


elseif ($field == "artwork")

Link to comment
Share on other sites

This

if ($result != "0")

 

will always be true, even if query returns no rows.

 

Use mysql_num_rows() instead.

 

And your error is caused because you actually put your else inside if code block.

 

This is where it's supposed to be

if ($result != "0")
            {
               $count = count(0);


               //grab all the content

   
               while ($r=mysql_fetch_array($result))

               {   
   
                  //the format is $variable = $r["nameofmysqlcolumn"];

                  //modify these to match your mysql table columns

         
                  $fullName=$r["fullName"];


         
                  //display the row

               echo $count++; echo ". ".$fullName." <br>";

               }
         
}
else
            {
               echo "Nothing found.";
            }

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.