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? :-\

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.

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;

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.