Ok I am working on a simple page and navigation system. Basically, I store all the links for the navigation panel in a MySQL database. The table has 3 fields, 'id', 'nav_name' and 'nav_link'. I want to have dynamic links like index.php?page=blahblahblah. The information to be displayed is also in a table.
The problem is, I cant preset all of the if queries, checking what the $_GET['page] variables is because the links are going to change. If new ones are entered, I would have to go in and change the code to reflect the new possible string after the = as well.
So I thought about fetching everything, then inserting each fetched 'nav_name' into an array. Then I could check whats in and move on from there.
Am I trying to do things the hard way and there is an easier way or am I screwed? If you know of anything, please help me out.
Thanks in advanced,
Sam!
(PHP) MySQL fetched rows into an array?
Started by Supersword, Jul 11 2007 04:57 PM
2 replies to this topic
#1
Posted 11 July 2007 - 04:57 PM
#2
Posted 11 July 2007 - 08:36 PM
Well, its not a bad idea, and is simple to implement. Its actually just a simple dynamic white list pretty much.
Of course you could also just use mysql_num_rows() if you needed to change the query to actually pull some information, instead of just a COUNT().
// MySQL connection assumed
// Also assuming $page is cleaned from the $_GET variable
$query = mysql_query("SELECT COUNT(*) AS count FROM `nav` WHERE `nav_name` = '{$page}'") or die(mysql_error());
$result = mysql_fetch_assoc($query);
if($result['count'] > 0){
// It exists, process content
}
else{
// Failed, echo error or redirect, etc
}
Of course you could also just use mysql_num_rows() if you needed to change the query to actually pull some information, instead of just a COUNT().
Edited by Demonslay, 11 July 2007 - 08:38 PM.
#3
Posted 13 July 2007 - 03:21 PM
So let me get this straight. You have a some pages stored in MySQL that have links corresponding to each page that are stored in MySQL?
If that is the case you can avoid having multiple tables to define one item. What you could do is the table you have you could add in a column for a name which you name the page and another column with link name in which you define what the link text will be. You could also go as far as adding a column for ordering.
Then with some code for the link you could do this.
That will display your links for the same table is your pages so when you update of delete or add a page you wont have to double your work.
If that is the case you can avoid having multiple tables to define one item. What you could do is the table you have you could add in a column for a name which you name the page and another column with link name in which you define what the link text will be. You could also go as far as adding a column for ordering.
Then with some code for the link you could do this.
<?php
$select = mysql_query("SELECT 'name','link_name','order' FROM 'pages' ORDER BY 'order' ASC") or die(mysql_error());
while($link = mysql_fetch_array($select))
{
echo "<a href='?page="' . $link["name"] . '"'>"' . $link["link_name"] . '"</a><br />";
}
?>
That will display your links for the same table is your pages so when you update of delete or add a page you wont have to double your work.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
