0

I get some data from my database, with a query and then I loop it via a while loop:

$r = mysql_query("SELECT * FROM advertisement_packages");  
while ($a = mysql_fetch_assoc($r)):
    echo $a['exposure]; //This prints out 1,2,3,4
endwhile;

How can I do, so:

  • exposure = 1 = Mini
  • exposure = 2 = Standard

etc. etc.

2
  • 1
    What? Can you please formulate your question? Commented Aug 10, 2011 at 15:06
  • You want to print out 'exposure = 1', 'exposure = 2'? Commented Aug 10, 2011 at 15:09

4 Answers 4

3
if ($a['exposure'] == 1){
    echo "Mini";
}
elseif($a['exposure'] == 2){
    echo "Standard";
}
Sign up to request clarification or add additional context in comments.

Comments

0
$labels = array(1 => 'Mini', 2 => 'Standart');
echo $labels[$a['exposure']];

Comments

0

It might be helpful to store the values independently instead of using a (possibly) enormous if structure:

$exposures = array(
    1    => 'Mini',
    2    => 'Standard',
    // and so forth
);

while($a = mysql_fetch_assoc($r)):
    echo $exposures[ $a['exposure'] ];
endwhile;

Just my $0.02 to make the code a bit more legible.

Comments

0

**Another good way to do this is to maintain a table structure in the database that associates the exposure number to its description. This way you can display the exposure description straight from your database (you can also populate a drop-down menu on web forms from the exposure_info table). Just follow the example SQL to see if you understand. **

CREATE TABLE photo_clients( id INT AUTO_INCREMENT PRIMARY KEY, client_name VARCHAR(100), exposure TINYINT );

CREATE TABLE exposure_info( exposure INT AUTO_INCREMENT PRIMARY KEY, description VARCHAR(100) );

INSERT INTO exposure_info (description) VALUES ('mini'), ('standard'), ('poster');

INSERT INTO photo_clients (client_name, exposure) VALUES ('John Doe', 2), ('Jane Smith', 1), ('Johnny Walker', 3);

SELECT a.client_name AS client, b.description AS exposure FROM photo_clients a, exposure_info b WHERE a.exposure = b.exposure;

The output of the above statement should look something like:

client          exposure
------------------------
Jane Smith      mini
John Doe        standard
Johnny Walker   poster

1 Comment

I endorse all of this advice except for the comma-JOIN syntax.

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.