Other Software > Developer's Corner
Use a foreach loop maybe?
kevstero:
I want my table to only list the fields that have the same SubId as the one chosen in the dropdown menu by the user. Lets call this variable $Test.
How can I only show the fields in this table where SubId = $Test?
Maybe a foreach loop? Somebody care to help?
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td>SubId</td>
<td>CatId</td>
<td>SubName</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['SubId']; ?></td>
<td><?php echo $row_Recordset1['CatId']; ?></td>
<td><?php echo $row_Recordset1['SubName']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
kevstero:
What's wrong with this code?
<?php for ($row_Recordset1['SubId'] = $Test);
do { ?>
<tr>
<td><?php echo $row_Recordset1['SubId']; ?></td>
<td><?php echo $row_Recordset1['CatId']; ?></td>
<td><?php echo $row_Recordset1['SubName']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
mouser:
your for part is no good.
you could simply do a check INSIDE your do while loop (which incidentally should probably be a while do loop)
and say
if ($row_Recordset1['SubId'] != $Test) continue;
OR you coud modify the mysql so that it only fetched rows with that setting.
housetier:
Just add the appropriate WHERE-clause to your database query and then following code should work
--- Code: PHP ---$table = '<table>';foreach($recordset as $row) { $table .= '<tr>'; $table .= '<tr>'. $row['SubId'] .'</td>'; $table .= '<td>'. $row['CatId'] .'</td>'; $table .= '<td>'. $row['SUbName'] .'</td>'; $table .= '</tr>';}$table .= '</table>'; print $table;
If you can't change your database query, you can skip over the $rows that don't match the selected 'SubId' in $test:
--- Code: PHP ---$table = '<table>';foreach($recordset as $row) { if ($row['SubId'] != $test) continue; $table .= '<tr>'; $table .= '<tr>'. $row['SubId'] .'</td>'; $table .= '<td>'. $row['CatId'] .'</td>'; $table .= '<td>'. $row['SUbName'] .'</td>'; $table .= '</tr>';}$table .= '</table>'; print $table;
But it is really the best if you make the query as specific as possible.
kevstero:
Thanks guys..
I couldn't get anything to work at all by using your code.. The foreach loop just wasn't working.. With this code, I was however able to still get my headers displayed, only no content goes into the table.. The variable $Test does have a value, so it should list something in that table.. Any idea what's wrong? I'm guessing the only relevant code to look at is at the bottom of this. Dreamweaver puts in a lot of stuff..
--- Code: PHP ---<?php require_once('Connections/antiques.php');$Test = $_POST['select2'];?><?phpif (!function_exists("GetSQLValueString")) {function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue;}} mysql_select_db($database_antiques, $antiques);$query_Recordset1 = "SELECT * FROM itemsubcategory";$Recordset1 = mysql_query($query_Recordset1, $antiques) or die(mysql_error());$row_Recordset1 = mysql_fetch_assoc($Recordset1);$totalRows_Recordset1 = mysql_num_rows($Recordset1);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head> <body><table border="1" cellpadding="0" cellspacing="0"> <tr> <td>SubId</td> <td>CatId</td> <td>SubName</td> </tr> <?php do { if ($row_Recordset1['SubId'] != $test) continue; ?> <tr> <td><?php echo $row_Recordset1['SubId']; ?></td> <td><?php echo $row_Recordset1['CatId']; ?></td> <td><?php echo $row_Recordset1['SubName']; ?></td> </tr> <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?></table></body></html><?phpmysql_free_result($Recordset1);?>
Navigation
[0] Message Index
[#] Next page
Go to full version