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
https://forums.phpfreaks.com/topic/91680-functions-in-variables-help/
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 />") ?>

 

 

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,

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.