1

Currently I have the following section of code:

<?php if($sales_pages): ?>
    <?php foreach($sales_pages as $sale): ?>
<div id ="sales">
    <span class"thumbnail"><a href="<?=base_url()?>page/sales/viewsale/<?=$sale->id?>"><img class="thumbnailImg" src="<?=base_url()?>includes/uploads/sales/thumbs/<?=$sale->thumbname?>" alt=""/></a>
    <span class="location"><?= $sale->location?></span>
    <span class="price"><?= $sale->price?></span>
    </span>
            </div>

    <?php endforeach; ?>

What I am trying to do is the following:

When there are 0 sales (fetched from database) in the foreach I would like my code to display a message

Is elseif the correct way to do this?

3 Answers 3

3

No. You should use else. It's basically the same as saying elseif(True). You'll end up with something like:

<?php if($sales_pages): ?>
    <?php foreach($sales_pages as $sale): ?>
        Print a sale
    <?php endforeach; ?>
<?php else: ?>
    Nothing here...
<?php endif; ?>

You can (and should) read more about the control structures in PHP in the PHP documentation.

Oh, and by the way. It looks like you are using CodeIgniter. Did you know that you can use the site_url function instead of prepending stuff with base_url()? You'd then write the url of your link like this (although I think you have a syntax error in that part of your code above):

<a href="<?=site_url("page/sales/viewsale/$sale")?>">
Sign up to request clarification or add additional context in comments.

Comments

1

Use ELSE.

$numberOfSale = //fetched from database;

if($numberOfSale ==0)
{
    //Display here your message
}
else
{
    //Loop
}

Comments

1
$result = mysql_query("SOME SQL HERE") or die("Could not perform select query - " . mysql_error());

    $num_rows = mysql_num_rows($result);


    if($num_rows == NULL)
    {
        return NULL;
    }
    else
    {
       //Loop it and use the results 
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.