Jump to content

Functions in Variables Help


SajanParikh

Recommended Posts

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.

Link to comment
Share on other sites

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 />") ?>

 

 

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.