Forum Moderators: coopster

Message Too Old, No Replies

Dealing with MySQL arrays

         

andrewheiss

9:29 pm on Mar 15, 2008 (gmt 0)

10+ Year Member



I'm trying to build a basic CMS with related MySQL tables. HTML is taken out of a MySQL table and put in a page according to a get variable. I'm trying to limit what get variables are allowed and want to store the allowed pages in another table.

Currently this is my set up:

$allowedPages = array("home", "about", "registration","contact");
$allowedPageTitles = array("Home","About","Registration","Contact");
$allowedPagesFull = array_combine($allowedPages1, $allowedPageTitles);

$pageName = NULL;

if (isset($_GET['pageSubmit'])) {
if (in_array($_GET['page'], $allowedPages)) {
$pageName = $_GET['page'];
}
}

The two separate arrays could just be one associative array since I'm combining them later. The arrays hold the html name and the visible name for each page.

Ideally I'd like to store those arrays in a table and somehow reference them via a query that lets me combine them into an associative array or something, like this (this code obviously doesn't work though:

$allowedPages = mysql_query("SELECT id_page, title FROM $tableName");
if (isset($_GET['pageSubmit'])) {
if (whatever the GET variable was is allowed by the $allowedpages query)) {
$pageName = $_GET['page'];
}
}

What's the best way to do this?

Thanks!

jatar_k

1:22 pm on Mar 16, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not following totally so I'll just chat a little

when I look at those 3 arrays it looks like a single table to me. Why do you want to store the arrays? Could you not store all those attributes as columns and each set as an individual row?

andrewheiss

4:33 pm on Mar 16, 2008 (gmt 0)

10+ Year Member



Sorry, that explanation was confusing.

The arrays were my original workaround for the problem, not using any tables: I built the associative array from the two separate arrays for htmlname and realname:


$allowedPages = array("home", "about", "registration","contact");
$allowedPageTitles = array("Home","About","Registration","Contact");
$allowedPagesFull = array_combine($allowedPages, $allowedPageTitles);

And then I could use those arrays in my code, checking the GET value against them. If GET doesn't match anything in $allowedPages, the default page is shown, though

That only works for one page, though, which is why I want to get that info into a table, potentially structured like this:


¦id¦htmlname¦realname¦
¦1 ¦ home ¦ Home ¦
¦2 ¦ about ¦ About ¦
etc

This way, the users can add new pages--an easy CMS thing to do.

The only thing I can't figure out is how to check if the GET value that is passed is in that htmlname column. inarray doesn't seem to work looking in an array generated by mysql_fetch_array when I try something like this:


$allowedpages = mysql_fetch_array(mysql_query("SELECT htmlname FROM allowedpages"));
if (in_array($_GET['page'], $allowedPages)) {
$pageName = $_GET['page'];
} else {
$pageName = "home";
}

That doesn't work though. How can I check GET against the results of that one column?

Hopefully this makes more sense...

Thanks!

cameraman

6:29 pm on Mar 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Feel free to thump me if I'm stating the extremely obvious to you.
mysql_fetch_array fetches one row of columns and returns them to you as an array. If you want more than one row of data you need to set up a control loop like while to retrieve them into an array that you construct yourself.

andrewheiss

11:32 pm on Mar 16, 2008 (gmt 0)

10+ Year Member



Oops - I forgot to put that in my explanation. I was trying to make an associative array with mysql_fetch_array, like "SELECT htmlname, realname FROM allowedpages" and check that with inarray

What would be the best way to create an array like "home => Home, about => About" from all the rows in the table? Is that the most efficient way to check the GET value or is there a better way to see if something is in the table?

cameraman

8:35 pm on Mar 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about
"SELECT htmlname FROM allowedpages WHERE htmlname='" . mysql_real_escape_string($_GET['page']) . "'"

If mysql_num_rows is zero, use your default. If not, the supplied page name is allowed.