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
https://forums.phpfreaks.com/topic/132761-solved-nested-ifs/
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
https://forums.phpfreaks.com/topic/132761-solved-nested-ifs/#findComment-690444
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
https://forums.phpfreaks.com/topic/132761-solved-nested-ifs/#findComment-690447
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.