Accessing array data from mysql database and php -
i have 2 mysql tables information:
one table stores page info name , location array column store widgets show per page stored in array format: 1,2,3,4
the other table actual widget table widget info , id identifier ex: 1,2,3,4
i need to:
get array info page table loop through array , display content widget table.ive been working foreach statements:
the url each page follows format index.php?page=thispage&route=thisroute $_get['page']
$page = $_get['page']; $order = "select * pages name='$page'"; $result = mysql_query($order); while($row = mysql_fetch_array($result)){ $widget = array(); $widget .= $row[3]; } print_r($widget); foreach($widget $w) { $order = "select * menu id='$w'"; $result = mysql_query($order); while($row = mysql_fetch_array($result)){ print $row[1]; } }
update: figured out solution works me.
the reason behind keeping comma separated values because have 1 widget per page or 15 widgets per page , each widget can added on fly. , administration side want able select widgets each page can have.
when keeping info in comma separated values info in 1 column "1,2,3,4".
ill stick exploding info array , looping through each value of array:
$page = $_get['page']; $order = "select * pages name='$page'"; //select widgets show on page $result = mysql_query($order); while($row = mysql_fetch_array($result)){ $widget = $row[3]; $string = preg_replace('/\,$/', '', $widget); //remove trailing comma $array = explode(', ', $string); foreach($array $value) //loop on values { $order = "select * widget id='$value'"; $result = mysql_query($order); while($row = mysql_fetch_array($result)){ print $row[1]; //print data: formated specific divs set up. } } }
get array info page table: select array_column_name table_store_page id=your_id
(use sql fetch array in)
execute sql statement in php maybe using mysql_query
anyway, storing array/comma-separated values in database never recommended. can seek normalizing database.
php mysql arrays
No comments:
Post a Comment