Welcome to WebmasterWorld Guest from 50.19.156.133

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Display bound array in PHP

     

yaelede

6:00 pm on Dec 7, 2011 (gmt 0)

10+ Year Member



Hi,

The following array is defined in an include file. They represent facilities of one row of a mysql table:

<?php 

$facilitiesgroup1=array();
$facilitiesgroup2=array();

$facilitiesgroup1[1]="Red";
$facilitiesgroup1[2]="Black";
$facilitiesgroup1[3]="Wite";
$facilitiesgroup1[4]="Green";

$facilitiesgroup2[1]="Swimming";
$facilitiesgroup2[2]="Bowling";
$facilitiesgroup2[3]="Tennis";
$facilitiesgroup2[4]="Football";

?>


How can I display these on the page of a specific record (recordset)?

Each element of an array is bound to a mysql field (true/false), and I would like to display array elements which are checked in db.
(Sorry for being not too accurate, I'm not so good even in questioning php issues, since I'm just trying to convert an old asp site to php, and I'm much better in VBscript)

penders

9:15 am on Dec 8, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



What do you mean by "bound"? How are you 'binding' a boolean field value with non-boolean elements in your array?

yaelede

10:17 am on Dec 8, 2011 (gmt 0)

10+ Year Member



I mean that e.g. $facilitiesgroup1[1]="Red" element is represented in a database table by 'facilitiesgroup1_1', and when it is checked (true), than that property (record) has this facility (Red).

That's what I would like to display in the individual property page in a grouped list, like:

<p><strong>Facilities-Group-1:</strong><br />
<?php
echo implode($facilitiesgroup1,', ');?>
</p>
<br />
<p><strong>Facilities-Group-2:</strong><br />
<?php
echo implode($facilitiesgroup2,', ');?>
</p>


Of course the above code alone doesn't do the job, beacuse it lists all facilities defined in the array, regardless of what is checked in that row of the database.

penders

11:31 am on Dec 8, 2011 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Assuming your data structure is out of your control, then you can achieve this using variable variables [uk3.php.net]...

<?php  
// $row is the result set from executing your DB query
$row = array();
$colName = 'facilitiesgroup1_1';
$colValue = '';
$row[$colName] = true; // Dummy data
if ($row[$colName]) {
$colParts = explode('_',$colName);
$colValue = ${$colParts[0]}[$colParts[1]]; // Variable variables
}
echo $colValue; // "Red"
?>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month