Forum Moderators: coopster

Message Too Old, No Replies

Links Page with categories

         

adammc

4:48 am on Sep 25, 2004 (gmt 0)

10+ Year Member



Hi,
I am kinda new to PHP and have created a link site script.
I now want to be able to categorise the links, but im not too sure how do do it.

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

jamie

7:32 am on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi adammc,

can you already add links using the add.php and can you already access the db and echo out the links to an html page?

adammc

7:48 am on Sep 25, 2004 (gmt 0)

10+ Year Member



Jamie,
Yep, got all that covered, have got an admin script to edit and delete entries too.

mincklerstraat

8:04 am on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This really depends entirely on the script you are using. I assume it already has the ability to put your links into categories - and you're adding your links to these categories when you add links. Funny it doesn't have an 'admin' option to just add categories via the browser (like links). Anyways, I'd look around in the 'admin' part of the script just using your browser and clicking around first, see if there's a readme or other instructions on using the script, and if that doesn't work, look in the php files to see if the categories are added there somewhere, and if not, get phpmyadmin to snoop around in the db tables and see if you can't add them there. Phpmyadmin might also be avaialble from your hosting company - if you're in cpanel, click on 'mysql', and see somewhere at the bottom of the page.

jamie

8:14 am on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i second the phpmyadmin idea - it is a simple matter to add fields to a database using it.

adammc

8:15 am on Sep 25, 2004 (gmt 0)

10+ Year Member



mincklerstraat,
I dont have categories at all. This is a redesign of a simple guestbook script that i made.

adammc

8:17 am on Sep 25, 2004 (gmt 0)

10+ Year Member



I have access to php my admin.
But the script at the moment only has these tables :

Title
Url
Description

I guess I need to add a table for the categories?
But what then?

jamie

10:40 am on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

mincklerstraat

10:43 am on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry adammc, didn't see you did this script yourself - this question is usually asked by people who get a script from somewhere. No simple answer here - you'll need to add another table, yes, and then have a WHERE somewhere in your query, that's fed by a variable you input via $_GET. Solution will probably involve reading the beginners' tutorial at php.net. Congrats, though, on writing your own script. The knowledge you've already gained will help you out here.

adammc

11:23 am on Sep 25, 2004 (gmt 0)

10+ Year Member



Thanks for the tips guys!
I have managed to put together the adding and viewing part :)

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 ("&nbsp;&nbsp;$title<br>&nbsp;&nbsp;Url: $url<BR>&nbsp;&nbsp;Category: $category<BR>&nbsp;&nbsp;Description: $description<BR>\n");
}
echo ("</DT></P>");
}
?>

jamie

12:08 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi adam,

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 .= "&nbsp;&nbsp;$title<br>&nbsp;&nbsp;Url: $url<BR>&nbsp;&nbsp;&nbsp;Description: $description<BR>\n";
}
elseif ($row['category'] == 'car_sites') {
$car_sites .= "&nbsp;&nbsp;$title<br>&nbsp;&nbsp;Url: $url<BR>&nbsp;&nbsp;&nbsp;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)

mincklerstraat

12:15 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On the php page which displays links only from a particular category, try this sql:
/* $cat = the category name or id you are using, you can set it to $_GET['cat'] or whatever you are using as the parameter in your url's */
$SQL = 'SELECT * FROM links WHERE category="'.mysql_escape_string($cat).'"';

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>

adammc

9:46 pm on Sep 25, 2004 (gmt 0)

10+ Year Member



Hi Guys,
I implimented what Jamie said and keep getting errors.

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 .= "&nbsp;&nbsp;$title<br>&nbsp;&nbsp;Url: $url<BR>&nbsp;&nbsp;&nbsp;Description: $description<BR>\n";
}
elseif ($row['category'] == 'Catering Companies') {
$Catering_Companies .= "&nbsp;&nbsp;$title<br>&nbsp;&nbsp;Url: $url<BR>&nbsp;&nbsp;&nbsp;Description: $description<BR>\n";
}
elseif ($row['category'] == 'Catering Supplies') {
$Catering_Supplies .= "&nbsp;&nbsp;$title<br>&nbsp;&nbsp;Url: $url<BR>&nbsp;&nbsp;&nbsp;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>

jamie

8:16 am on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



adam

probably the missing semi-colon after the echo <table>"

this is a very common error (i still get loads ;-)

added: if you know it's not the line in question, it is almost certainly a missing bracket } or ; on the line before

adammc

8:59 am on Sep 26, 2004 (gmt 0)

10+ Year Member



Added a ; still getting the error :(

echo "<table>
<tr>
<td>$Chef_Supplies</td>
<td>$Catering_Companies</td>
<td>$Catering_Supplies</td>
</tr>
</table>";

mincklerstraat

9:02 am on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My nose says missing bracket - try jedit, available on most platforms, it'll highlight your php syntax real nice to give you a better idea of what's what (variable, function, etc. etc.), and when you have the cursor on a bracket, parentheses, or brace, it will show you the corresponding opening or closing bracket, parentheses, or brace. Sweet!

adammc

10:29 am on Sep 26, 2004 (gmt 0)

10+ Year Member



Am downloading the program now, thanks for the tip ;)

Does this bracket need to be closed as i cant see it closing anywhere?

while ($row = mysql_fetch_array($result)){

adammc

10:59 am on Sep 26, 2004 (gmt 0)

10+ Year Member



Hi mincklerstraat,
You were right! I found the missing bracket with Jedit, thanks for your help ;)

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)){

jamie

11:25 am on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi adam,

that means $result is empty

mincklerstraat

11:40 am on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



so surround it all in an if clause:
if($result) {
/* your code */
}

adammc

10:12 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



mincklerstraat & jamie,
I got it! Thansk you soooooo much, you guys rock!

I now just have to get the admin (edit / delete entries) working ~fingers crossed~

adammc

12:39 am on Sep 27, 2004 (gmt 0)

10+ Year Member



In my admin and add.php script I have had to manually add the category names into the coding of both scripts.

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>

jamie

5:39 am on Sep 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi adam,

>> thanks

no problem...

re cats: search google for populating a drop down menu from an array or a database.

have a look at some of the tutorials at zend and devshed. i'd say you have more than enough skills to solve most problems now with a bit of thought.

good luck