SajanParikh Posted February 18, 2008 Share Posted February 18, 2008 Hello, I need a quick consult for something I'm working on...or trying to at least. Any help would be greatly appreciated. I have this function... <?php function dispCatAndWords($before, $after) { $query="SELECT * FROM categories"; $result=mysql_query($query); $num=mysql_numrows($result); if ($num == 0) { echo "There are no categories"; } else { $i=0; while ($i < $num) { $catname=mysql_result($result,$i,"name"); $words=mysql_result($result,$i,"words"); $id=mysql_result($result, $i, "id"); echo "$before" . $catname . " (" . $words . ")$after"; $i++; } } } ?> In there you can see the variable $id gets its value from the database. The problem is...I want to use that variable as an argument in this function itself. For example, here is what I'm currently trying to do. <?php dispCatAndWords("<a href=\"delete.php?id=" . $id . "\">", "</a><br />") ?> Everything works great, except it doesn't echo out the $id. I appreciate any help in advance. Quote Link to comment Share on other sites More sharing options...
redarrow Posted February 18, 2008 Share Posted February 18, 2008 dont forget every page needs the session_start() function/statement on all pages useing the session['id'] ....... <?php session_start(); function dispCatAndWords($before, $after) { $query="SELECT * FROM categories"; $result=mysql_query($query); $num=mysql_numrows($result); if ($num == 0) { echo "There are no categories"; } else { $i=0; while ($i < $num) { $catname=mysql_result($result,$i,"name"); $words=mysql_result($result,$i,"words"); $_SESSION['id']=mysql_result($result, $i, "id"); echo "$before" . $catname . " (" . $words . ")$after"; $i++; } } } ?> <?php session_start(); dispCatAndWords("<a href=\"delete.php?id=" . $_SESSION['id'] . "\">", "</a><br />") ?> Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 18, 2008 Share Posted February 18, 2008 so you want the function to replace $id with the $id from the query? there are multiple ways of doing this, the easiest to code that i can think of is to change the function like so: <?php function dispCatAndWords($before, $after) { $query="SELECT * FROM categories"; $result=mysql_query($query); $num=mysql_numrows($result); if ($num == 0) { echo "There are no categories"; } else { $i=0; while ($i < $num) { $catname=mysql_result($result,$i,"name"); $words=mysql_result($result,$i,"words"); $id=mysql_result($result, $i, "id"); echo $before.$id."\">".$catname." (" . $words . ")$after"; // notice the difference. $i++; } } } ?> you will then call this function like so: <?php dispCatAndWords("<a href=\"delete.php?id=", "</a><br />") ?> ------------------ if you would like to keep the functionality of the function (if you dont want to rewrite all of your function calls), you could try this: <?php function dispCatAndWords($before, $after) { $query="SELECT * FROM categories"; $result=mysql_query($query); $num=mysql_numrows($result); if ($num == 0) { echo "There are no categories"; } else { $i=0; while ($i < $num) { $catname=mysql_result($result,$i,"name"); $words=mysql_result($result,$i,"words"); $id=mysql_result($result, $i, "id"); $result = "$before" . $catname . " (" . $words . ")$after"; // store in variable instead of echoing yet. echo str_replace("#ID_VAR#",$id,$result); $i++; } } } ?> then you will call the function like so: <?php dispCatAndWords("<a href=\"delete.php?id=#ID_VAR#\">", "</a><br />") ?> -- hope this helps, Quote Link to comment Share on other sites More sharing options...
SajanParikh Posted February 18, 2008 Author Share Posted February 18, 2008 Wow, that was quick. I got to work now too. Thanks a ton! Thank you both for the quick responses. redarrow, I can see how sessions would be useful here. uniflare, I used your second method, as I did want the function to...function the same. Wow, again. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.