Jump to content

if function parameter is ON, do something


V

Recommended Posts

I making a function that queries DB table items and I would like to also show the total rows if a variable totalRows is set or "on".

 

This is what I came up with..

 

//above is the $connection variable for DB connection

function anyQuery($connection, $table, $column, $totalRows) {

$sql = "SELECT * FROM $table ORDER BY post_date DESC";
$result = $connection->query($sql) or die(mysqli_error($connection));

//find out how many records were retrieved
$numRows = $result->num_rows;

while ($row = $result->fetch_assoc()) {		
echo strtoupper("<li>{$row["$column"]}</li>");
}
if($totalRows == "on") {
	echo $totalRows;
}
}

 

It echoes the table items fine but echoes "on" instead of the rows number.

 

I tried the function like this

 

 

anyQuery($connection, "posts", "post_title", "on");

 

I even tried setting the variable to "on" before calling the function and still no go. Does someone have a better idea? :-\

Link to comment
Share on other sites

Mchl I'm sorry, I was re-writing the code before posting. I changed numRows to totalRows but I still get no total row number :(

 

gwolgamott, thanks for the suggestion! I use a mysql num_rows function to get the same result. It works fine when I remove the "if" statement from the function.

Link to comment
Share on other sites

Oh I see, you were trying to echo numRows and not totalRows...

You meant this:

while ($row = $result->fetch_assoc()) {
echo strtoupper("<li>{$row["$column"]}</li>");
}
if($totalRows == "on") {
echo $numlRows;
//that explains why you were gettting "on" all the time then, but not why the if doesn't work
}
}

 

And that if statement doesn't work at all?

I've a suspicion that totalRows is not getting set correctly.

//echo the totalRow here just to be sure it is being sent to the function with something you believe it should be

function anyQuery($connection, $table, $column, $totalRows) {
echo $totalRows;

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.