Kyrus Posted May 14, 2008 Share Posted May 14, 2008 Hello. I'm rather new to php, been reading here and there, editing some pre-made codes etc. Just so you know my bases. Let's go direct to the point then. I need to echo the descriptions of the products in the cart page of my website, so far so good, but there are some products that don't have descriptions, instead they have models, sizes, etc. What I need to do is to build a code that checks if the product has description, if so print it, if not, then print the model, if theres no model then print the size. Something like this. Anyone knows how to do this? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 14, 2008 Share Posted May 14, 2008 The idea would be to check to see if the description field is empty - if it isn't, print out the description. If it is, print out the model and size etc. So it would be something like: <?php $sql = "SELECT id,description,model,size FROM yourtable"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ echo 'Product ID: '.$row['id']; echo '<br />'; if(!empty($row['description'])){ echo $row['description']; }else{ echo 'Model: '.$row['model'].' Size: '.$row['size']; } echo '<br /><br />'."\n"; } ?> P.S. Welcome to the forum Quote Link to comment Share on other sites More sharing options...
Kyrus Posted May 15, 2008 Author Share Posted May 15, 2008 Hello Ben. <?php $sql = "SELECT id,description,model,size FROM yourtable"; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ echo 'Product ID: '.$row['id']; echo '<br />'; if(!empty($row['description'])){ echo $row['description']; }else{ echo 'Model: '.$row['model'].' Size: '.$row['size']; } echo '<br /><br />'."\n"; } ?> This is a bit too much for me. I applied it but it gives me all the models and Sizes of ALL products. The page just extends forever considering the amount of products I have in the DB. I need a simple solution. My code at the moment is as simple as this: <? echo $descri; ?> I've already set $descri to the description field in the database, and set $mod and $tam for model and size too. All I need now is a code that says something like this: If $descri field is empty then check $mod, if $mod is empty too then check $tam, if $tam empty then check $blablabla. If every field is empty then don't print anything. This code is already on a table in the exact position to be printed, no need to farmat the text, just print the data it finds on the DB. Thanks for your time. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted May 15, 2008 Share Posted May 15, 2008 Stick the variables in an array and cycle through till you find a non-empty one: <?php $var1 = ''; $var2 = ''; $var3 = 'foo'; $var4 = 'bar'; $vars = array($var1,$var2,$var3,$var4); foreach($vars as $v){ if(!empty($v)){ echo $v; break; } } ?> Quote Link to comment Share on other sites More sharing options...
Kyrus Posted May 19, 2008 Author Share Posted May 19, 2008 $var1 = ''; $var2 = ''; $var3 = 'foo'; $var4 = 'bar'; What am I supposed to write after the = ? I wrote: $var1 = ''; $var2 = 'descricao'; $var3 = 'mod'; $var4 = 'tam'; But once I put it online I get "descricao" on the field I want to print. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.