Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to put an 'if' statment within an echo
Old 03-13-2005, 09:39 AM how to put an 'if' statment within an echo
cforscutt's Avatar
Novice Talker

Posts: 9
Trades: 0
I’m using the following code to pull back featured shops from my database at random and I want an 'if' statement to display an image if its available. I have tried the following but I must be doing something wrong as its not functioning within the echo giving an error, I put it down to the double quotes <? if ($shop_pic != '') { echo "$shop_pic."; } ?> but still had no joy tweaking is there something I’m missing, any ideas how to get this working would be gratefully appreciated:

<?
include("include/dbconnect.php");

$result = mysql_query("SELECT * FROM $table ORDER BY rand($mtm) desc limit " . MAX_DISPLAY_NEW_PRODUCTS);
echo "<table width=100% cellpadding=2 cellspacing=1 border=0><tr>";

$alternate = "2";
while ($myrow = mysql_fetch_array($result))
{
$shop_name = $myrow["shop_name"];
$id = $myrow["id"];
$shop_short_descr = $myrow["shop_short_descr"];
$shop_pic = $myrow["shop_pic"];

if ($alternate == "1") {
$color = "#ffffff";
$alternate = "2";
}
else {
$color = "#cadeff";
$alternate = "1";
}
echo "<td width=33%>$shop_short_descr <br> <? if ($shop_pic != '') { echo "$shop_pic."; } ?></td>";
}
echo "</tr></table>";
?>
cforscutt is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-13-2005, 09:49 AM
xoomcity's Avatar
Skilled Talker

Posts: 61
Location: Penang, Malaysia
Trades: 0
You mean a graphic image display or filename of the image? If the former, then does your $shop_pic contains the tag "<img src=" upfront? A far shot - I am also a beginner...
__________________
Today and tomorrow will quickly become yesterday. Time waits for no one... So, wake up and start living now! Hence,
Please login or register to view this content. Registration is FREE
was born...
xoomcity is offline
Reply With Quote
View Public Profile Visit xoomcity's homepage!
 
Old 03-13-2005, 10:14 AM
cforscutt's Avatar
Novice Talker

Posts: 9
Trades: 0
I realise I would need the <img src="$shop_pic"> but wanted to simplify things for the post, its the format of the if statement within the echo thats not working!!
cforscutt is offline
Reply With Quote
View Public Profile
 
Old 03-13-2005, 10:18 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Remember that you don't need to print out a single line of mark-up with a single line of PHP. Also, you are trying to open up a new PHP block
when you are already in one.

Try this:

PHP Code:
echo "<td width=33%>$shop_short_descr <br>";

if ( ! empty( 
$shop_pic ) ) 

    echo 
"$shop_pic"
}

echo 
"</td>"
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-13-2005, 11:41 AM perfect
cforscutt's Avatar
Novice Talker

Posts: 9
Trades: 0
thats exactly what I was after cheers Kyrnt
cforscutt is offline
Reply With Quote
View Public Profile
 
Old 03-13-2005, 11:42 AM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Trades: 0
this is an example

PHP Code:

if ( $yourvariable TRUE );

{

echo 
"It works!!";


dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-14-2005, 04:09 AM
Experienced Talker

Posts: 36
Trades: 0
dan please don't post incorrect code, an example has already been provided. Not only do you have a syntax error, you are also doing assignment instead of comparasion.
tress is offline
Reply With Quote
View Public Profile
 
Old 03-14-2005, 09:30 AM
cjdesign's Avatar
Average Talker

Posts: 15
Location: England
Trades: 0
Quote:
Originally Posted by dan245
this is an example

PHP Code:

if ( $yourvariable TRUE );

{

echo 
"It works!!";


DOH!
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
cjdesign is offline
Reply With Quote
View Public Profile Visit cjdesign's homepage!
 
Old 03-14-2005, 09:33 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Ok, dan posted some bad code -- let's not dwell on it. Anyone who posts to these forums will invariably do that -- I know I have. The point is let's not celebrate it.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-14-2005, 09:40 AM
Gaffer Sports's Avatar
Ultra Talker

Posts: 397
Name: Steve
Location: Scotland
Trades: 1
Well said, Kyrnt.

Not too far from this thread is one about our biggest mistakes, which proves we all make them. Atleast the guy was trying to help.

Steve.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
Gaffer Sports is offline
Reply With Quote
View Public Profile Visit Gaffer Sports's homepage!
 
Old 03-14-2005, 11:36 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
I think I made the = versus == mistake TWICE in the same thread when posting up example code. When coding my own stuff it would error and I'd find it and fix it, but it's not helpful to the poor soul who trys my code and gets the error. For most complicated things I try and knock up a test script so I know i works.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-14-2005, 02:32 PM
camperjohn's Avatar
Ultra Talker

Posts: 268
Location: San Diego
Trades: 0
Remember you CAN use the ? inside another statement. ? acts like an inline if statement:

echo "test: " . ((a == true) ? "yes" : "no");

If a == true, it will print yes, if it is false, it will print no.

JM
__________________

Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
camperjohn is offline
Reply With Quote
View Public Profile Visit camperjohn's homepage!
 
Old 03-14-2005, 04:16 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
Of course that's true -- it's called the tertiary operator. It is most appropriate to use when choosing between achieving one or two values for the purposes of assignment or printing. It is less appropriate when the function is printing (or echoing) and one of the values is null or empty string. Such would have been the case here, so a traditional "if" statement is most appropriate here. But your way is equally effective. The original problem would have looked like this:

PHP Code:
echo empty( $shop_pic ) ? "" $shop_pic
While equally functional, it does unnecessarily print an empty string when $shop_pic is empty and is not as immediately readable as the traditional if. But it is a valid solution.

With that said, it would be considerably more elegant if you had a placeholder image indicating that an image was not available (itself an image of course) named, for example, notAvail.jpg. Then you could do something like this:

PHP Code:
$unavailImg "images/notAvail.jpg";
echo empty( 
$shop_pic ) ? $unavailImg  $shop_pic
Or better yet, put all that logic into a function and just call that:

PHP Code:

// Within code, I want to display an image if available, otherwise a standard "not available" image.
$shop_pic "images/myCoolPic.jpg";
displayImg$shop_pic );

// ...... OR
$shop_pic null;
displayImg$shop_pic );   // will display the alternate image




function displayImg$img )
{
    
$unavailImg "images/notAvail.jpg";
    
$img = empty($img) ? $unavailImg  $img;

    print 
"<img src=\"$img\" />";



__________________
—Kyrnt

Last edited by Kyrnt; 03-15-2005 at 08:15 AM.. Reason: Clarification in code
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Reply     « Reply to how to put an 'if' statment within an echo
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.67642 seconds with 12 queries