DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Use a foreach loop maybe?  (Read 9,119 times)

kevstero

  • Participant
  • Joined in 2008
  • *
  • Posts: 16
Use a foreach loop maybe?
« on: June 28, 2008, 03:32 AM »
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

  • Participant
  • Joined in 2008
  • *
  • Posts: 16
Re: Use a foreach loop maybe?
« Reply #1 on: June 28, 2008, 04:36 AM »
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

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Use a foreach loop maybe?
« Reply #2 on: June 28, 2008, 06:53 AM »
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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,321
    • Gathering Badge
Re: Use a foreach loop maybe?
« Reply #3 on: June 28, 2008, 08:25 AM »
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

  • Participant
  • Joined in 2008
  • *
  • Posts: 16
Re: Use a foreach loop maybe?
« Reply #4 on: June 28, 2008, 01:30 PM »
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'];
?>
<?php
if (!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>
<?php
mysql_free_result($Recordset1);
?>

kevstero

  • Participant
  • Joined in 2008
  • *
  • Posts: 16
Re: Use a foreach loop maybe?
« Reply #5 on: June 29, 2008, 03:23 AM »
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.

Mouser, you're the man.. Your if statement worked like a charm.. For anybody who uses this as a reference, this is what worked:

<?php do { ?>
    <tr>
<?PHP if ($row_Recordset1['SubId'] != $Selection) continue; ?>
      <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>

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Use a foreach loop maybe?
« Reply #6 on: June 29, 2008, 04:11 AM »
its still not the cleanest code and your do-while loop is a bit awkward because you basically doing this:

line 37: $row_Recordset1 = mysql_fetch_assoc($Recordset1);
then some other lines and then:
do {stuff} while while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));


When it would be cleaner to simply delete line 37 and have:

while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)) do { stuff }

Original address: https://www.donationcoder.com/forum/index.php?topic=13861.0