Jump to content

Retrieving data from MySQL specific fields in a cycle


Kyrus

Recommended Posts

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
    }
}
?>

Link to comment
Share on other sites

$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.

 

:(

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.