homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Display bound array in PHP
yaelede




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

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




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

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

yaelede




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

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




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

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"
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved