Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP Array from mysql table

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 8 '09
I am currently trying to make a website. I have an array that is for the variable $page. It currently looks like

Expand|Select|Wrap|Line Numbers
  1.  if ($page == logout){include ('logout.php');}
  2.   $pages = array(
  3.  
  4.         'register'        => 'regform.php',
  5.         'verify'        => 'verify.php'
  6.     );
  7.  
  8.     if (array_key_exists($page, $pages))
  9.         include($pages[$page]);
  10.  
I am trying to put all of this into an sql table, which currently looks like

Expand|Select|Wrap|Line Numbers
  1. +----+----------+-------------+
  2. | id | name     | page        |
  3. +----+----------+-------------+
  4. |  1 | register | regform.php |
  5. |  2 | verify   | verify.php  |
  6. +----+----------+-------------+
I have tried a few different pieces of code but so far nothing has worked.

if you can advise, that would be great

TheServant's Avatar
Expert
 
Join Date: Feb 2008
Location: Australia
Posts: 914
#2: Oct 8 '09

re: PHP Array from mysql table


How about a foreach loop? I will do it for MySQL, but logic is the same for SQL...
Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO table (";
  2. $values = " (";
  3.  
  4. foreach($pages as $name=>$page) {
  5. $query .= $name . ", "; // To get: register, verify, etc...
  6. $values .= "'$page', "; // To get: 'regform.php', 'verify.php', etc...
  7. }
  8.  
  9. /* remove last commas and replace with closing brackets */
  10. $query = substr_replace($query ,")",-2);
  11. $values = substr_replace($values ,")",-2);
  12.  
  13. $query_string = $query . $values;
  14.  
  15. /* Run $query_string */
Or something to that effect.
Reply

Tags
array, page, php, sql, table