Forum Moderators: coopster

Message Too Old, No Replies

Select certain rows in database

         

dbzfyam

1:06 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



I'm currently working on a sort of CMS for my site. I want to have a sort of WYSIWYG textbox (downloaded a premade scrit somewhere) where I can write new pages. It also should update the database (insert the new page and automatically add a new link for the navigation in the database). The problem is, I can't get PHP to select the right rows to display. I currently have this:

case "site":
echo "<BR><BR>SUBMENU:";
$query = "select * FROM links WHERE name IS 'site' ORDER BY name";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{echo "<br />¦ <a href=\"{$row['url']}\">{$row['name']}</a> ¦";
}
break;

Which doesn't seem to work. I want the PHP script to select the rows where the column 'name' contains 'site' (So that I can use a switch to show the right links for the right category).

First, I had this:

SELECT category, url, name FROM links

Which, of course, selects all links currently in the database.
Can anyone help me with this? It's probably something simple which I've forgotten.

Thanks in advance,
Stefan.

mcibor

1:19 pm on Jun 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you want only rows with name containing 'site' then the query is such:

SELECT category, url, name FROM links WHERE name LIKE '%site%' ODRDER BY name;

BTW you can use mysql_fetch_assoc(), not array with MYSQL_ASSOC
Best regards!
Michal Cibor

coopster

1:20 pm on Jun 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>>I want the PHP script to select the rows where the column 'name' contains 'site'

SQL has a lot of String Comparison Functions to help you locate only the rows you want. The LIKE predicate is one of them.

$query = "SELECT * FROM links WHERE name LIKE '%site%' ORDER BY name";

Since you seem to be using MySQL:
[dev.mysql.com...]

<edit>Thanks Michal ;)

dbzfyam

2:02 pm on Jun 3, 2005 (gmt 0)

10+ Year Member



Thank you both for the help! It seems to be working now.

Mcibor, should the code be
mysql_fetch_assoc($result)
instead of what I have right now?

I have another problem right now though. On the main page where the content will go, I have the following code:

<?php if($page == "") { include "content/site/news.txt"; } else { include ($page.".txt"); }?>

However, when I include the PHP navigation, it interferes with the content part. When I click on a link, it shows the links it finds in the database, but since the URLs are like this:

http://localhost:85/layout/main2.php?page=content/site/news

The menu script causes the content to load the starting page (news.txt) instead of keeping the current content and just displaying the new links. The code of the navigation:

<?
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'site';
$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

echo"MENU:<br /><b><a href=\"?menu=site\">Site</a><br /> <a href=\"?menu=information\">Information</a><br /> <a href=\"?menu=media\">Media</a><br /> <a href=\"?menu=fun\">Fun</a><br /> <a href=\"?menu=reviews\">Reviews</a><br /> <a href=\"?menu=cheats\">Cheats</a><br /> <a href=\"?menu=webmasters\">Webmasters</a><br /></b>";

function index()
{
}
$choice=$_GET['menu'];

switch($choice)
{

case "site":
echo "<BR><BR>SUBMENU:";
$query = "SELECT * FROM links WHERE category LIKE '%site%' ORDER BY name";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{echo "<br />¦ <a href=\"{$row['url']}\">{$row['name']}</a> ¦";
}
break;

etc.etc.

default:
index();
}
?>

Do you know how I can let these two scripts work together, so it keeps the content and displays the menu instead of going to main.php?menu=site (so it keeps [localhost:85...] and still loads the menu somehow)? I can't seem to figure out how to do this since adding the menu part at the end of the URL results in a 404 error.

Thanks,
Stefan.