php while loop with row variable - php

Not sure why this will not work? Does the loop not like my variable? If I hard code this workings...
while($row = mysql_fetch_array($resultno))
{
echo "<tr>";
echo "<td>" . $row['$radio'] . "</td>";
echo "<td>" . $row['count'] . "</td>";
echo "</tr>";
}

'$radio' is a string, not a variable. Remove the apostrophes and make it into $radio:
$row[$radio]
This will make it possible to choose a column from the MySQL resultset by setting $radio to the chosen value.

Your row is not likely to have a $radio key; maybe you meant $row['radio']. Or $row[$radio].

Your array referencing is wrong:
$row['$radio']
should be written as follows, when referencing an array element using another variable
$row[$radio]
or as follows when accessing an element name
$row['radio']

$row['$radio'] seems to be a problem. Is $radio a variable with a value you would like to use as index for the $row then write $row[$radio] otherwise if the table column is named "radio" only write $row['radio'].

Related

PHP displays only one word from MySQL record in URL variable

A PHP page displays only one word from a MySQL record in a URL variable, while it displays the correct 2 words in the HTML output, I tried many solutions such '".$row['book_category']."' and {$row['book_category']} etc. but I still get the same one result.
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "" . $row['book_category'] . "" . "</td>";
echo "</tr>";
}
?>
So now first $row insert is only one word in the url, while the second $row outputs two words properly as expected; the problem is I need the two words to be passed as variable in the URL.
URLs cannot have a space character, this is why you are seeing the first word only, you need to encode the value received using rawurlencode().
<?php
foreach($conn->query('SELECT book_category, COUNT(*) FROM books GROUP BY book_category') as $row) {
echo "<tr>";
echo "<td>" . "<a href='sidebar_cat_display.php?book_cat=" . rawurlencode($row['book_category']) . "'>" . $row['book_category'] . "</a>" . "</td>";
echo "</tr>";
}
?>
Keep in mind you will also need to adjust the sidebar_cat_display.php page to fetch the book_cat parameter from the URL accordingly if you aren't already.

Trying to pass variable to URL in the edit link of show results

I have a show results page dumping the contents of a table. I've managed to echo in an edit link on each of my table rows. Edit kicks user to an update records page.
I'm trying to include the variables from the table row to the edit URL so I can do a $_GET URL param function on the update page and propagate the update form.
Here's what I have at the moment:
echo "<table>";
/* Get field information for all fields */
while ($row = mysqli_fetch_object($queryResult))
{
echo "<tr>";
echo "<td>" . $row->band_name . "</td><td>" . $row->votes . "</td><td><a href='vote.html?$band_name&$votes'>Edit</a></td>";
echo "</tr>";
}
First off, make sure that you declare your variables properly. It seems these guys are undefined in your url:
$band_name&$votes
Then format your url properly:
while ($row = mysqli_fetch_object($queryResult))
{
$band_name = $row->band_name;
$votes = $row->votes;
echo "<tr>";
echo "
<td>$band_name</td>
<td>$votes</td>
<td>
<a href='vote.html?band_name=".urlencode($band_name)."&votes=$votes'>Edit</a>
</td>";
echo "</tr>";
}
Are you sure its <a href='vote.html? HTML? you won't be able to parse this properly. Change it to vote.php
Then in vote.php, just get those values thru $_GET:
<?php
if(isset($_GET['band_name'], $_GET['votes'])) {
$band_name = $_GET['band_name'];
$votes = $_GET['votes'];
// rest of code mysqli etc. etc.
}
?>
You're not very clear on exactly what is not working, but in order for PHP to be able to get the query params they have to have a name.
if you change the following line and add parameter names, you should be able to access the data with $_GET or parse_str.
echo "<td>" . $row->band_name . "</td><td>" . $row->votes . "</td><td><a href='vote.html?band_name=$band_name&votes=$votes'>Edit</a></td>";

Switch between different views using href

I'm coming here after some head banging. Still learning php/mysql and coming across an issue which is bothering me for a while.
Currently I print a table with all the data without any condition. The values below the lower values and above are printed in this table. The snippet of my code is below:
while ($result = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$result['id']. "</td>";
if ($result['time'] > $limit[lower]){
echo "<td bgcolor=#ff0000>" $result['time']. "</td>";
} else {
echo "<td>" .$result['time']. "</td>";
}
echo "<td>" .$result['name']. "</td>";
echo "</tr>";
}
I want to create a href link so that by dafault only values above the lower are printed and on clicking the link, say detailed_view will give the table as it does above i.e. all value including below and above the lower value. Shall I create two methods and use them in href separately. In that case there will be two while loops which I want to avoid. Any pointer on this?
The name of the script is display.php, so before the loop I tried using:
echo "<a href='display_jobid.php?view=$num'>Show All</a>";
It does create a hyperlink Show All but how to get the functionality working, am not sure.
Thanks

php return multiple variables from other page

I'm trying to create a table with links that return a 'mf_id' value and its corresponding 'Manufacturer' value. I can do one at a time, but when I try to combine the two, problems begin to crop up. Here's what I have so far:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Manufacturer'] . "</td>";
echo "</tr>";
}
and the other page:
$numb = $_GET['mf_id'];
$name = $_GET['Manufacturer'];
echo "<h1>$name</h1>";
$result=mysql_query("select * from Products where mf_id=$numb");
Thanks in advance!
Because you never pass Manufacturer through your querystring, the second page doesn't have access to it via GET. Also, for validity purposes, your querystring values should be passed through urlencode().
This line:
echo "<td>" . $row['Manufacturer'] . "</td>";
Should be:
echo "<td>" . $row['Manufacturer'] . "</td>";
Please Note: It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.
UPDATE:
Per meagar's advice I learned about http_build_query(). This is definitely the way to go when writing querystrings to URLs:
$data = array('mf_id' => $row['mf_id'], 'Manufacturer' => $row['Manufacturer']);
echo "<td><a href='list.php?" . http_build_query($data) . "'>" . $row['Manufacturer'] . "</a></td>";
This doesn't make any sense at all: $row['mf_id'&&'Manufacturer']. That is not how you access two elements of an array. You're combining two strings with &&, yielding boolean true, and attempting to access $row[true]. You can't access an array that way.
If you want to use both items, you need to access them individually:
$row['mf_id'] . $row['Manufacturer']
If you want to build a query string containing these two values, you should use http_build_query which will take care of URL-encoding your data:
$query = http_build_query(array('mf_id' => $row['mf_id'], 'manufacturer' => $row['Manufacturer']));
echo '<td>' . $row['Manufacturer'] . '</td>';
Note that, if you actually just select the fields you need, you don't have to explicitly specify them in the arguments to http_build_query. If your $row already contains only mf_id and manufacturer, it would be enough to use
$query = http_build_query($row);
You're only passing mf_id into the page, you are not passing Manufacturer.
Edit (as you've changed your code)
Change:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Manufacturer'] . "</td>";
echo "</tr>";
}
To:
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Manufacturer'] . "</td>";
echo "</tr>";
}

PHP how to print proper column td1 td2 td3 output?

I am trying to output a table using php variables like this:
echo "<tr>";
echo "<td>".$var1."</td>";
echo "<td>".$var2."</td>";
echo "</tr>";
It works fine if both variables exist.. but if $var1 is null, the $var2 value is outputted to the firs column in the HTML table.
Is there a way to properly output the columns by naming them?
P.S: I can't do "if $var == NULL ...." for some coding reasons. I want to control the HTML output if possible.
While I agree with #leeb above, do you want something like this:
echo "<td>" . isset($var1)?$var1:"" . "</td>";
echo "<tr>";
echo "<td>".$var1." </td>";
echo "<td>".$var2." </td>";
echo "</tr>";

Categories