PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Wed Feb 12, 2003 7:14 pm
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
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Feb 12, 2003 7:50 pm
which part of the selection troubles you?
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Wed Feb 12, 2003 8:10 pm
I'm not having any troubles, I just don't know what code to use... Do you know?
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed Feb 12, 2003 8:26 pm
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?
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Feb 12, 2003 9:49 pm
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.
Last edited by
volka on Wed Feb 12, 2003 9:51 pm, edited 1 time in total.
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Wed Feb 12, 2003 9:51 pm
I'll try it, thanks!
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Wed Feb 12, 2003 9:56 pm
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>
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Feb 12, 2003 9:59 pm
using my example you changed your script to what?
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Wed Feb 12, 2003 10:00 pm
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!
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Wed Feb 12, 2003 10:10 pm
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?
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Thu Feb 13, 2003 2:57 am
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!
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Thu Feb 13, 2003 3:00 am
Hi, there are differences between the different versions of PHP 4 - if you run the following code:
what does it say?
Mac
Mr. Tech
Forum Contributor
Posts: 205 Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia
Post
by Mr. Tech » Thu Feb 13, 2003 3:02 am
4.2.0