Web Hosting Talk







View Full Version : PHP MySQL Quick Help


SimNetwork001
12-30-2008, 12:43 PM
Hi,
I have a forum coming along and I am nearlly done.
What I am having trouble with is this.
On the index page how would I list the
Categories and then under the categories, I want the forums relevant to the category.
But I dont how to link the queries. I know how to query the categories but not how to query the forums to go under them.
Please help. Many thanks.

tim2718281
12-30-2008, 01:04 PM
... I want the forums relevant to the category.
Create a table with two columns, one for forums, one for categories.
Insert one row in the table for each forum-category pair.
Then to list the forums for category x, select from the table the forums where category equals x.

SimNetwork001
12-30-2008, 02:25 PM
How would I do that code wise though ?

Eye4Designs
12-30-2008, 02:31 PM
$res = mysql_query("SELECT forumid FROM forums WHERE cat=$catid") or sqlerr(__FILE__, __LINE__);
This pretty much says select a forum in the dbase where the catagory is given this id

SimNetwork001
12-30-2008, 02:33 PM
Thanks I will try that out now.

Eye4Designs
12-30-2008, 02:46 PM
of course change the variables in the query string to match yours...
you can also add ORDER BY and LIMIT if needs be...
Sorry if it's not needed.. it's just i don't know your level of php
I would do this as a full code block
$res = mysql_query("SELECT forumid FROM forums WHERE cat=$catid") or sqlerr(__FILE__, __LINE__);
$arr = mysql_fetch_array($res);
$catforum = $arr["forumid"];
as you can see you can now assign many different variables from the query... forum name description etc etc
this is fairly simple code like i mentioned i don't know your level of php

SimNetwork001
12-30-2008, 06:18 PM
Where you have the "WHERE cat=$catid"
There is nothing for $catid, so i get the;
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\myprogram\xampp\htdocs\xampp\tryme.php on line 13
Error. I have sent have had to attach the file that im editing to the post and hope you can help.

Eye4Designs
12-30-2008, 09:31 PM
Yes because you haven't updated the forums to include the new db field catid...
I'll look at your file and repost
You will need to add a new field in your dbase in the forums
I'll give you the SQL line code to run after

Eye4Designs
12-31-2008, 01:11 AM
try this
<HTML>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<?php
$host="localhost"; // Host name
$username="user"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$res = mysql_query("SELECT * FROM forums WHERE ParentID=1");
while($row = mysql_fetch_array($res)){
print("<td width=53% align=center bgcolor=#E6E6E6><div align=left><a href=view_forums.php?id=$rows['ID']>$rows['Name']</a></div></td>\n");
}
mysql_close();
?>
</tr>
</table>
</body>
</html>
This depends soley on that in the dbase parentid has a value of 1
parentid seems to be the category id

Eye4Designs
12-31-2008, 03:10 AM
I can't edit the code so gotta repost just caught my eye
<HTML>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<?php
$host="localhost"; // Host name
$username="user"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$res = mysql_query("SELECT * FROM forums WHERE ParentID=1");
while($row = mysql_fetch_array($res)){
print("<td width=53% align=center bgcolor=#E6E6E6><div align=left><a href=view_forums.php?id=$row['ID']>$row['Name']</a></div></td>\n");
}
mysql_close();
?>
</tr>
</table>
</body>
</html>
just changed $rows to $row

SimNetwork001
12-31-2008, 06:52 AM
Nope, natta. Not a working. Still get an error saying...
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\myprogram\xampp\htdocs\xampp\testcategories.php on line 19
Strange.

Xenatino
12-31-2008, 07:00 AM
<html>
<body>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<?php
$host="localhost"; // Host name
$username="user"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="forum"; // Table name
// Connect to server and select databse.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name)or die("cannot select DB");
$res = mysql_query("SELECT * FROM forums WHERE ParentID='1'");
while($row = mysql_fetch_array($res))
{
echo "<td width=\"53%\" align=\"center\" bgcolor=\"#E6E6E6\">
<div align=\"left\">
<a href=\"view_forums.php?id=" . $row['ID'] . "\">" . $row['Name'] . "</a>
</div>
</td>\n";
}
mysql_close();
?>
</tr>
</table>
</body>
</html>
Just a tidy-up of Eye4Design's code - try that

SimNetwork001
12-31-2008, 07:27 AM
That half works but I get an error saying.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\myprogram\xampp\htdocs\xampp\testcategories.php on line 18
Which refers to the while function. Not sure why though but at least it is showing the table. Just getting the data.

Eye4Designs
12-31-2008, 03:37 PM
Do you have numeric values in the dbase for parentid ??
If so what are the numbers in that field ?
That is 80% of the reason you have a fetch error...

SimNetwork001
01-01-2009, 07:15 AM
Ok, here is what I have. I have sorted that out but what I need is if the record has a ParentID of 1 then it comes out bold not normal.
I know its possible...Can you help ?