Output data based on true or false

I’m a little stuck outputting data from a table like this. This is a table for profiles.

CREATE TABLE `prod_profiles` (
 `p_add` VARCHAR(300) NULL DEFAULT NULL,
 `p_add_s` TINYINT(1) NULL DEFAULT '0',
 `p_phone` VARCHAR(300) NULL DEFAULT NULL,
 `p_phone_s` TINYINT(1) NULL DEFAULT '0',
 //and many more
  )
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

Over here, p_add is the address, p_add_s (anything with the _s) is the part which decides if a user would like to display this data or not (0 is display, 1 is hide).

I need to display data like: (this is the admin section)

Address data goes here if (p_add_s = 0) {echo ‘Hide’;} else {echo ‘Unhide’;}
Phone data goes here if (p_phone_s = 0) {echo ‘Hide’;} else {echo ‘Unhide’;}
My problem is how do i tell php to print the first column and then check the _s columns value with using all the columns names in php like below. I’m lost here.

The way I’m doing it now is:

 while($form_data = mysql_fetch_array($exe_prof_form))
    {
    echo $form_data['sc_p_add']; if ($form_data['sc_p_add_s'] ==0){echo "Hide";}else{echo "Show";
    //Remaining fields go here
    // Any way I can get rid of these fields and do what I'm trying?
    }

Any idea how to get this right?

I think you are asking how to reduce all the extra typing you would have to do if you have many similar fields in your database, ie “something”, “something_s”

This will do it, although it relies on you maintaining an array of field names to which _s is to be appended:


// $rows spoofs your mysql result set for this example

$rows[0]['add'] = "my add";
$rows[0]['add_s'] = 0;  // should show
$rows[0]['phone'] = "1234567";
$rows[0]['phone_s'] = 1;  // should NOT show

$rows[1]['add'] = "your add";
$rows[1]['add_s'] = 1;  // should NOT show
$rows[1]['phone'] = "7654321";
$rows[1]['phone_s'] = 0;  // should show

// this is a hardcoded array, which could be included from some other place
// if you could work out how to use this to, say, generate the html form

$values = array('add','phone');

// loop through result set

foreach( $rows as $row){
  foreach($values as $value){
   echo $row[$value] ;
   echo ( $row[$value .'_s'] ) ? ' Show' : ' Hide' ;  // uses a ternary operator
   echo PHP_EOL;  // new line char
  }
}
// gives: 
// my add Hide
// 1234567 Show
// your add Show
// 7654321 Hide

If you had say, 20 such fields in your dbase, then this would obviously save you a lot of time, typing and errors, and as I say, I think this was the gist of your question.

HTH

Well, I came up with this: (Warning: Ugly Code ahead)

select p_add, p_add_s, p_phone, p_phone_s

while($row = mysql_fetch_array($exe_prof_form)) {
for ($i=0; $i<mysql_num_fields($exe_prof_form);$i+=2)
{
echo $i." ".$row[$i];
$r=$i+1;
echo ($row[$r] == 0) ? " Hide <br />" : " Show <br />";
}
}

Does the job… Tell me what you think.

Your solutions good too. We’ll have to update the array each time tough.

I think your solution is tied to always doing the truth test on every 2nd row, which might be just fine - but thats the trade off you would have to make rather than maintain an array of expected values.

My solution assumed (perhaps wrongly) that you were bringing back multiple rows.

I suppose there would be another way which detected the appearance of “_s” at the end of the key and only then did the truth test - that might provide the best of both worlds.

ps I’d be very cautious of this test:


$row[$r] == 0

Prefer a check for both type AND value with the extra = sign.


$row[$r] === 0