Forum Moderators: coopster
I do know that I will need to add a category field into the database and in my add.php page
Can anyone point me in the dirction of a tutorail that may help me find the answers?
Regards
Adam
you probably have 1 table with 3 fields. in pypmyadmin, you need to look at the structure of the table and from that page insert another field 'category' after the description. it doesn't matter what you call it, but try to stick to a consistent naming scheme.
after that it's a case of modifying the add.php (and edit.php) so that you can insert a category into the database as well as the title, url and description.
How do I view only the links in a certain category?
<?
# setup SQL statement
$SQL = " SELECT * FROM links ";
# execute SQL statement
$retid = mysql_db_query($db, $SQL, $cid);
# check for errors
if (!$retid) { echo( mysql_error()); }
else {
# display results
while ($row = mysql_fetch_array($retid)) {
$title = $row["title"];
$url = $row["url"];
$category = $row["category"];
$description = $row["description"];
echo (" $title<br> Url: $url<BR> Category: $category<BR> Description: $description<BR>\n");
}
echo ("</DT></P>");
}
?>
i would take the echo-ing out of the while loop and instead gather each category of link into it's own variable using .=, whilst testing each result as you loop through it:
while ($row = mysql_fetch_array($result)){
if ($row['category'] == 'dating sites') {
$dating_sites .= " $title<br> Url: $url<BR> Description: $description<BR>\n";
}
elseif ($row['category'] == 'car_sites') {
$car_sites .= " $title<br> Url: $url<BR> Description: $description<BR>\n";
}
(etc, etc...)
the variables $dating_sites and $car_sites now contain all the links whose category field was set to dating_sites or car_sites.
then it is a matter of displaying those variable where you want in your html code below:
echo "<table>
<tr>
<td>$dating_sites</td>
<td>$car_sites</td>
</tr>
</table>"
does that help?
(edited for spelling)
That said, you really should look up stuff first when you get some hints like above - this forum isn't really meant as a replacement for learning PHP, but an aid in learning it. Note here that there are double quotes between the single quotes, and that we use mysql_escape_string to 'clean' the value to make sure nasty scriptkiddies don't get through to our precious database and add queries of their own to display our private information. If this sounds like Chinese to you, make sure you read up on database security and php before you do too much scripting, or you could be vulnerable to what's called 'cross-site scripting.'
Happy coding!
<edit> I see that jamie has posted a solution above since I started writing this response which is really a good bit simpler than this one - a better one for newbie coding I think, try that one instead and learn WHERE's later! </edit>
Parse error: parse error, unexpected $ in /path/view2.php on line 100
line 100 is "</TR></TBODY></TABLE></BODY></HTML>"
<?
# setup SQL statement
$SQL = " SELECT * FROM links ";
# execute SQL statement
$retid = mysql_db_query($db, $SQL, $cid);
# check for errors
if (!$retid) { echo( mysql_error()); }
else {
# display results
while ($row = mysql_fetch_array($result)){
if ($row['category'] == 'Chef Supplies') {
$Chef_Supplies .= " $title<br> Url: $url<BR> Description: $description<BR>\n";
}
elseif ($row['category'] == 'Catering Companies') {
$Catering_Companies .= " $title<br> Url: $url<BR> Description: $description<BR>\n";
}
elseif ($row['category'] == 'Catering Supplies') {
$Catering_Supplies .= " $title<br> Url: $url<BR> Description: $description<BR>\n";
}
echo "<table>
<tr>
<td>$Chef_Supplies</td>
<td>$Catering_Companies</td>
<td>$Catering_Supplies</td>
</tr>
</table>"
?>
<br><br></td>
</TR></TBODY></TABLE></BODY></HTML>
Only problem now is that I am getting this error :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /path/view2.php on line 75
Line 75 is:
while ($row = mysql_fetch_array($result)){
If I want to add more categories later I am going to have to change all the code as well :(
Is there anyway i can just pull the category names from the database?
<?
# this is processed when the form is submitted
# back on to this page (POST METHOD)
if ($REQUEST_METHOD=="POST") { # setup SQL statement
$SQL = " INSERT INTO links ";
$SQL = $SQL . " (title, url, category, description) VALUES ";
$SQL = $SQL . " ('$title', '$url','$category','$description') ";
#execute SQL statement
$result = mysql_db_query($db,"$SQL",$cid);
# check for error
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
echo ("<P><B>Your Entry Has Been Added</B></P>\n");
}
?>
<FORM NAME="fa" ACTION="add.php" METHOD="POST">
<TABLE>
<TR><TD><B>Title: </B> </TD><TD><INPUT TYPE="text" NAME="title" SIZE=40></TD></TR>
<TR><TD><B>Url:</B> </TD><TD><INPUT TYPE="text" NAME="url" SIZE=40></TD></TR>
<TR><TD><B>Category:</B> </TD><TD><SELECT name="category" class="textbox">
<OPTION value="Chef Supplies">Chef Supplies</OPTION>
<OPTION value="Catering Companies">Catering Companies</OPTION>
<OPTION value="Catering Supplies">Catering Supplies</OPTION>
</SELECT></TD></TR>
<TR><TD VALIGN=TOP><B>Description: </B> </TD><TD> <TEXTAREA NAME="description" ROWS=5 COLS=40></TEXTAREA></TD></TR>
<TR><TH COLSPAN=2><P><INPUT TYPE="submit" VALUE="Add"></P></TH></TR>
</TABLE>
</FORM>
<? mysql_close($cid);?>
<br><br></td>
</TR></TBODY></TABLE></BODY></HTML>