How Do You Get One Variable to Show in Place of Another if the First is Null?

Discussion in 'PHP' started by Masterful, Sep 5, 2008.

  1. #1
    How can I get a variable from another table to show in place of a first variable if the first variable is NULL? Basically, I want (see below) the coupon_code variable to show unless it is NULL, in which case I want the offer_instructions variable to show.

    I've already tried the code below, and it seems to work. However, I'm sure it's incorrect. All I done was put the two variables side-by-side. :eek:

    Any help, however small, will be awarded with reputation points! ;)

    --------------------------

    <?php

    $con = mysql_connect("localhost", "root");

    if (!$con)

    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db("ctyi", $con);

    $result = mysql_query("SELECT Coupon_Table.offer_anchor_text, Coupon_Table.offer_url, Coupon_Table.coupon_code,
    Offer_Instructions_Table.offer_instructions FROM Coupon_Table, Offer_Instructions_Table WHERE Coupon_Table.offer_instructions_id=Offer_Instructions_Table.offer_instructions_id");

    while($row = mysql_fetch_array($result))

    {
    echo "<table border=\"0\">";
    echo "<tr>";
    echo "<td><a href=\"" . $row['offer_url'] . "\">" . $row['offer_anchor_text'] . "</a><br />"
    . $row['coupon_code'] . $row['offer_instructions'] . "</td>";
    echo "</tr>";
    echo "</table>";
    }

    mysql_close($con);

    ?>
     
    Masterful, Sep 5, 2008 IP
  2. wdillsmith

    wdillsmith Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can't you just add an if statement? Like

    if (!empty($row['coupon_code']))
    echo $row['coupon_code'];
    else
    echo $row['offer_instructions'];
     
    wdillsmith, Sep 5, 2008 IP
    Masterful likes this.
  3. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    
    $con = mysql_connect("localhost", "root");
    
    if (!$con) die('Could not connect: ' . mysql_error());
    
    mysql_select_db("ctyi", $con);
    
    $query = <<<QUERY
    select
        coupon.offer_anchor_text,
        coupon.offer_url,
        coupon.coupon_code,
        instructions.offer_instructions
    from
        coupon_table as coupon,
    left join
        offer_instructions_table as instructions on
        instructions.offer_instructions_id = coupon.offer_instructions_id
    QUERY;
    
    $result = mysql_query($query);
    
    while ($row = mysql_fetch_array($result)) {
        $offerText = ($row['coupon_code'] != '') ? $row['coupon_code'] : $row['offer_instructions'];
        
        $output[] = "<table border=\"0\">";
        $output[] = "<tr>";
        $output[] = "<td><a href=\"{$row['offer_url']}\">{$row['offer_anchor_text']}</a><br />{$offerText}</td>";
        $output[] = "</tr>";
        $output[] = "</table>";
    }
    
    mysql_close($con);
    PHP:
     
    ahowell, Sep 5, 2008 IP
    Masterful likes this.
  4. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Thanks, guys! As promised, I have added reputation points to both of you. :)

    Ahowell, your method looked cool but didn't work, dude. It gave an error message, even with the PHP tag closed.

    Wdillsmith, your method worked, but only after I adapted it a bit (see below).

    Please tell me, guys . . .

    Should I use this code? Is it wise to put an If statement inside a While loop? Would you guys use it? If not, how would you change it?

    <?php
    
    $con = mysql_connect("localhost","root");
    
    if (!$con)
    
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("ctyi", $con);
    
    $result = mysql_query("SELECT 
    
    Coupon_Table.offer_anchor_text, 
    Coupon_Table.offer_url, 
    Coupon_Table.offer_expiration_date,
    Coupon_Table.coupon_code, 
    
    Advertiser_Table.advertiser_logo,
    Advertiser_Table.advertiser_name,
    Advertiser_Table.advertiser_url,
    
    Category_Table.category_name,
    Category_Table.category_url,
    
    Offer_Instructions_Table.block_anchor_text,
    Offer_Instructions_Table.offer_instructions
    
    FROM Coupon_Table, Advertiser_Table, Category_Table, Offer_Instructions_Table
    WHERE Coupon_Table.advertiser_id=Advertiser_Table.advertiser_id
    AND Coupon_Table.category_id=Category_Table.category_id
    AND Coupon_Table.offer_instructions_id=Offer_Instructions_Table.offer_instructions_id");
    
    while($row = mysql_fetch_array($result))
    
    	{
    	echo "<table border=\"0\">";
      	echo "<tr>";
     	echo "<td><img src=\"" . $row['advertiser_logo'] . "\" alt=\"\" /></td>";
      	echo "<td><a href=\"" . $row['offer_url'] . "\">" . $row['offer_anchor_text'] . "</a><br />";
    	[B]if (!empty($row['coupon_code']))
    	{echo $row['coupon_code'];}
    	else {echo $row['offer_instructions'];}[/B]
    	echo "<br /> Offer expires: " . $row['offer_expiration_date'] . "<br />";
            echo "About <a href=\"" . $row['advertiser_url'] . "\">" . $row['advertiser_name'] . "</a><br />";
     	echo "Category: <a href=\"" . $row['category_url'] . "\">" . $row['category_name'] . "</a></td>";
    	echo "<td><a href=\"" . $row['offer_url'] . "\">" . $row['block_anchor_text'] . "</a></td>";
    	echo "</tr>";
    	echo "</table>";
      	}
    
    mysql_close($con);
    
    ?>
    Code (markup):
     
    Masterful, Sep 5, 2008 IP
  5. nabz245

    nabz245 Well-Known Member

    Messages:
    677
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #5
    I would have made it a set variable just so the code looks neater. But it shouldn't really matter. Your going to need the IF statement inside the loop as each loops checks a different row in the sql db.




    <?php
    
    $con = mysql_connect("localhost","root");
    
    if (!$con)
    
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("ctyi", $con);
    
    $result = mysql_query("SELECT 
    
    Coupon_Table.offer_anchor_text, 
    Coupon_Table.offer_url, 
    Coupon_Table.offer_expiration_date,
    Coupon_Table.coupon_code, 
    
    Advertiser_Table.advertiser_logo,
    Advertiser_Table.advertiser_name,
    Advertiser_Table.advertiser_url,
    
    Category_Table.category_name,
    Category_Table.category_url,
    
    Offer_Instructions_Table.block_anchor_text,
    Offer_Instructions_Table.offer_instructions
    
    FROM Coupon_Table, Advertiser_Table, Category_Table, Offer_Instructions_Table
    WHERE Coupon_Table.advertiser_id=Advertiser_Table.advertiser_id
    AND Coupon_Table.category_id=Category_Table.category_id
    AND Coupon_Table.offer_instructions_id=Offer_Instructions_Table.offer_instructions_id");
    
    while($row = mysql_fetch_array($result))
    
    	{
    
    	[B]if (!empty($row['coupon_code'])) {
    	$var = $row['coupon_code'];
    	}else{
    	$var = $row['offer_instructions'];
    	}[/B]
    
    	echo "<table border=\"0\">";
      	echo "<tr>";
     	echo "<td><img src=\"" . $row['advertiser_logo'] . "\" alt=\"\" /></td>";
      	echo "<td><a href=\"" . $row['offer_url'] . "\">" . $row['offer_anchor_text'] . "</a><br />";
    	[b]echo $var;[/b]
    	echo "<br /> Offer expires: " . $row['offer_expiration_date'] . "<br />";
            echo "About <a href=\"" . $row['advertiser_url'] . "\">" . $row['advertiser_name'] . "</a><br />";
     	echo "Category: <a href=\"" . $row['category_url'] . "\">" . $row['category_name'] . "</a></td>";
    	echo "<td><a href=\"" . $row['offer_url'] . "\">" . $row['block_anchor_text'] . "</a></td>";
    	echo "</tr>";
    	echo "</table>";
      	}
    
    mysql_close($con);
    
    ?>
    Code (markup):
     
    nabz245, Sep 5, 2008 IP
    Masterful likes this.
  6. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Actually, there's a method to do this in a single line.

    echo (empty($row['coupon_code']) ? $row['offer_instructions'] : $row['coupon_code']);
    PHP:
     
    Shoro, Sep 5, 2008 IP
    Masterful likes this.
  7. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Nabz245 and Shoro, thank you both! I have added reputation points to both of your accounts. :)

    Shoro, cool response, dude! :cool:
     
    Masterful, Sep 6, 2008 IP