Jump to content

"php if" statement within a "php if" statement?


jacchops

Recommended Posts

Is it possible to have a "php if" statement within a "php if" statement? I don't know a whole lot about php, so I'm sorry if I use the wrong terms to describe things here. I'll try to be as specific as possible.

 

The website is for an art gallery that has multiple artists. I want to have a page for each artist that show samples of their work. This samples page will display small images in a table. The table will have a max of 3 images per column with a varying number of rows, depending on how many works of art each artist has.

 

I have designed it such that there is a sort of template page called artist_page.php. Then each artist has a php page that only contains variables in the code, such as a $image_1, $image_2, $image_3, etc. So let's say we are displaying works by Leonardo da Vinci. There would be a davinci.php page and the first variable would be $image_1="Mona_Lisa". On the page that lists all of the artists, you'd click on Leonardo da Vinci's name and it would take you to http://.../artist_page.php?artist=davinci. I'm not sure what that technique is called, but hopefully you all understand what I'm talking about.

 

I have added the code in the header of the artist_page.php to get the variables from the davinci.php so that I can echo them in my artist_page.php.

 

I want to set up the code on the artist_page.php so that the number of rows are dynamic and are based on the number of $image variables that are not null (or equal to "").

 

My idea was to set it up so that the artist_page.php would execute something like the following:

If the variable $image_1 is not null, then a row is created, and $image_1 populates the first cell in the row. Then (within this "if statement") I want to test to see if $image_2 is populated. If it isn't, then an empty cell is created. If $image_2 is populated, then it is outputted. Same thing with $image_3.

 

Then I want to test to see if the $image_4 is populated. If it is, then a new row is created and so on. If $image_4 is not populated, then our table closes.

 

Whenever I try to put the "if statement" for testing $image_2's value within the "if statement" for testing $image_1's value, I get an error. I have tried to do the code many different ways. Nothing works.

 

Perhaps I am setting this up the wrong way or perhaps my syntax is wrong. Any suggestions??

I didn't say this before, and I don't think it matters, but $image_1 is actually populated with a location for the image. When I run this code without the second and third <td>s, the $image_1 outputs correctly. But if I run it as it is below, I get the following error related to the line of code in the second <td>: "Parse error: syntax error, unexpected T_IF"

 

<?php if ($image_1!=="") {
echo 
'<tr>
    <td><a href="#"><img src="' . $image_1 . 'height="150" border="0" /></a></td>
    <td><a href="#"><img src="' . if ($image_2!=="") echo "$image_2" . 'height="150" border="0" /></a></td>
    <td><a href="#"><img src="' . if ($image_3!=="") echo "$image_3" . 'height="150" border="0" /></a></td>
  </tr>';
} ?>

 

Please use the CODE tags when posting code

<td><a href="#"><img src="' . if ($image_2!=="") echo "$image_2"

 

That's the problem - ="#"><img src="' . - the dot is to contiue echoing. You need to end with a ; and then put the if statement.

 

 

<?php 
if ($image_1!=="") 
{ //assuming that if there is no 1 there is no 2 either
echo '<tr>';
echo  '<td><a href="#"><img src="' . $image_1 . 'height="150" border="0" /></a></td>';

if ($image_2!=="")
{
echo '<td><a href="#"><img src="' . $image_2 . '" height="150" border="0" /></a></td>';
}

if ($image_3!=="")
{
echo '<td><a href="#"><img src="' . $image_3 . '" height="150" border="0" /></a></td>';
}

echo '</tr>';
}//ends first if
?>

What if I don't want the first "if... echo..." to stop? I actually have another row underneath this row that outputs the title of the image. That variable is $image_1_title, $image_2_title, etc. If there is no $image_2, then the #image_2_title will be equal to null.

 

So if I use a ";" to stop the echoing of the first "if statement", then the row with the image title's won't show up if there is no value for $image_2. But I only want that row to show up if there is at least an $image_1.

 

I figured that even if there is an $image_1 but no $image_2 or $image_3, I could still output the $image_2_title and the $image_3_title since they will just be empty values.

 

This is why I wanted to test for a value for $image_2 and $image_3 WITHIN the test for an $image_1. I hope that makes sense.

 

(And the assumption that there is no image 2 without an image 1 is correct.)

 

 

<?php if ($image_1!=="") {

echo

'<tr>

    <td><a href="#"><img src="' . $image_1 . 'height="150" border="0" /></a></td>

    <td><a href="#"><img src="' . if ($image_2!=="") echo "$image_2" . 'height="150" border="0" /></a></td>

    <td><a href="#"><img src="' . if ($image_3!=="") echo "$image_3" . 'height="150" border="0" /></a></td>

  </tr>

  <tr>

    <td>' . $image_1_title . '...</td>

    <td>' . $image_2_title . '...</td>

    <td>' . $image_3_title . '...</td>

  </tr>';

} ?>

 

 

OK - if you want it like that fair enough. But this bit will definately not work

 

<img src="' . if ($image_2!=="") echo "$image_2"

 

Remember that this is within an echo statement - you can't echo the if. You have to stop the old echo to run the if statement. Also the part after the if should be enclosed with curcly brackets - {}.

I'd suggests you use an array for this rather than individual variables, so taking your example:

<?php

$images = array();
// image 1
$images[0]['src'] = 'Mona_Lisa.jpg';
$images[0]['title'] = 'Mona Lisa';

// image 2
$images[1]['src'] = 'image2.jpg';
$images[1]['title'] = 'Image 2 title';

// image 3
$images[2]['src'] = 'image3.jpg';
$images[2]['title'] = 'Image 3 title';

// etc

?>

 

Now in to display the images just do:

echo '<table>';

if(is_array($images))
{
    // cut the $image array into sets of 3
    $rows = array_chunk($images, 3);

    // loop through the $rows array
    foreach($row as $col)
    {
        echo '<tr>';

        foreach($col as $image)
        {
            echo '<td>'.$image['title'].'</td>';
            echo '<td><img src="'.$image['src'].'" /></td>';
        }

        echo '</tr>';
    }
}

echo '</table>';

 

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.