Page 1 of 1
Link Directory - More help please
Posted: Wed Feb 12, 2003 7:14 pm
by Mr. Tech
Hi!
I need some more help with my Link Directory I am creating. In it, it have sub categories and main categories to put the links in.
Now what I need help with is when someone wants to go and edit a sub category and they click the link:
edit.php?maincat=3
in the MySQL database(Which I'm using), it will celect a main category with the id 3 in the selection box.
For and example go to:
http://www.webwondersnews.com/resources/add.php?sid=149
See how it selects the category you give?
How can I do that? What code do I need?
Thanks for any help!
Mr. Tech

Posted: Wed Feb 12, 2003 7:50 pm
by volka
which part of the selection troubles you?
Posted: Wed Feb 12, 2003 8:10 pm
by Mr. Tech
I'm not having any troubles, I just don't know what code to use... Do you know?
Posted: Wed Feb 12, 2003 8:26 pm
by patrikG
The way I understand your problem: you want the category displayed with the item?
Add another column named "category" to your "item"-table. Write the category-ID in that column and have your website pull that along with the item from the database.
Is that what you had in mind?
Posted: Wed Feb 12, 2003 8:40 pm
by Mr. Tech
I don't think so.
See when you click this link:
http://www.webwondersnews.com/resources/add.php?sid=149
it shows you in the selection box the category that has the id 149.
But if you click this(The same without the ?sid=149 in it) link:
http://www.webwondersnews.com/resources/add.php
It shows the defualt category.
Understand? That's what I want.
Thanks for your help!
Posted: Wed Feb 12, 2003 9:49 pm
by volka
still uncertain wether I got the point. Maybe something like this?
Code: Select all
<?php
$dbConn = mysql_connect(...);
mysql_select_db(..., $dbConn);
if (!isset($_GET['sid']) || (int)($_GET['sid']) < 1)
$_GET['sid'] = 0;
// creating form-element with GET-method
// creating select-element with name sid
$query = 'SELECT id, title, (id='.(int)$_GET['sid'].') FROM categories';
$result = mysql_query($query, $dbConn);
while($row=mysql_fetch_row($result))
echo '<option value="', $row[0], '"', ($row[2]) ? 'select="selected" >' : '>', $row[1], '</option>';
// continue form
?>
script not even tested by compiler.
SELECT ... (id=
value) ... returns 1 or 0 for this field depending on wether the condition is fulfilled or not.
Posted: Wed Feb 12, 2003 9:51 pm
by Mr. Tech
I'll try it, thanks!
Posted: Wed Feb 12, 2003 9:56 pm
by Mr. Tech
No, didn't work... This is the code I have now. It shows all the categories in the selection box.
Now when someone puts in the address bar:
add.php?cat=1
It will select the category in phpbiglinks_cats with the cat as 1.
Does this help?
<select name="newmaincat">
<option value="0">Select One</option>
<?php
$showcats = "select cat,title from phpbiglinks_cats where subcat='No'";
$showcatsresult = mysql_query($showcats) or die("Failed: $sql");
$numrows = mysql_num_rows($showcatsresult);
$half = intval($numrows / 1);
if (($half+$half)!=$numrows) $half = $half + 1;
print "";
for($x=0;$x<$numrows;$x++) {
$resrow = mysql_fetch_row($showcatsresult);
$cat = $resrow[0];
$title = $resrow[1];
$ctfolder = str_replace(" ", "_", $cat);
if ($x==$half) print "";
print "<option value=\"$cat\">$title</option>\n";
}
print "";
?>
</select>
Posted: Wed Feb 12, 2003 9:59 pm
by volka
using my example you changed your script to what?
Posted: Wed Feb 12, 2003 10:00 pm
by Mr. Tech
I'm still only a newbie and didn't know what to do... Do you have any suggestions on what I should add to that?
Thanks!
Posted: Wed Feb 12, 2003 10:10 pm
by volka
still completely untested, but you might try
Code: Select all
<select name="newmaincat">
<option value="0">Select One</option>
<?php
$newmaincat = (int)$_POST['newmaincat']; // might be $_GET['newmaincat'], depending on your form's method
$showcats = 'select cat,title,(cat='.$newmaincat.") from phpbiglinks_cats where subcat='No'";
$showcatsresult = mysql_query($showcats) or die("Failed: $sql :".mysql_error());
$numrows = mysql_num_rows($showcatsresult);
$half = intval($numrows / 1);
if (($half+$half)!=$numrows)
$half = $half + 1;
print ""; // what's that supposed to do?
for($x=0;$x<$numrows;$x++)
{
$resrow = mysql_fetch_row($showcatsresult);
$cat = $resrow[0];
$title = $resrow[1];
$selected = $resrow[2];
$ctfolder = str_replace(" ", "_", $cat);
if ($x==$half)
print ""; // what's that supposed to do?
echo '<option value="', $cat, '" ', ($selected) ? 'selected="selected" >':'>', $title, "</option>\n";
}
print "";
?>
</select>
btw: which version of php are you using?
Posted: Thu Feb 13, 2003 2:57 am
by Mr. Tech
No, didn't work, nothing was selected. My PHP version is version 4 or up...
Do you know that link I showed with the example, I think this is the code they use:
function view_category($return="",$relateview="",$compare="",$add_link="")
{
global $db, $Ssepchar,$Sarrow,$Stb_name;
if (!$return) $return = "sid";
if ($relateview) $add_query = "where relate<1";
if ($add_link) $add_query = "where c_link ='1'";
$db->query("select $return,pathtxt,relate from {$Stb_name}_CAT $add_query order by pathtxt");
for ($i=0 ; $i < $db->numrow ; $i++) {
$view_row = $db->nrecord();
$view_row[pathtxt] = str_replace($Ssepchar, $Sarrow , $view_row[pathtxt]);
if ($compare && $view_row[$return]==$compare)
$view_cat .="<option value='$view_row[$return]' selected>$view_row[pathtxt]</option>\n";
else
$view_cat .="<option value='$view_row[$return]'>$view_row[pathtxt]</option>\n";
}
return $view_cat;
}
That help?
THANKS!
Posted: Thu Feb 13, 2003 3:00 am
by twigletmac
Hi, there are differences between the different versions of PHP 4 - if you run the following code:
what does it say?
Mac
Posted: Thu Feb 13, 2003 3:02 am
by Mr. Tech
4.2.0