topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Sunday December 15, 2024, 1:57 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Use a foreach loop maybe?  (Read 7352 times)

kevstero

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 16
    • View Profile
    • Donate to Member
Use a foreach loop maybe?
« on: June 27, 2008, 10:32 PM »
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
  • *
  • default avatar
  • Posts: 16
    • View Profile
    • Donate to Member
Re: Use a foreach loop maybe?
« Reply #1 on: June 27, 2008, 11:36 PM »
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,914
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Use a foreach loop maybe?
« Reply #2 on: June 28, 2008, 01: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
  • ***
  • default avatar
  • Posts: 1,321
    • View Profile
    • Donate to Member
Re: Use a foreach loop maybe?
« Reply #3 on: June 28, 2008, 03:25 AM »
Just add the appropriate WHERE-clause to your database query and then following code should work
Code: PHP [Select]
  1. $table = '<table>';
  2. foreach($recordset as $row) {
  3.   $table .= '<tr>';
  4.   $table .= '<tr>'. $row['SubId'] .'</td>';
  5.   $table .= '<td>'. $row['CatId'] .'</td>';
  6.   $table .= '<td>'. $row['SUbName'] .'</td>';
  7.   $table .= '</tr>';
  8. }
  9. $table .= '</table>';
  10.  
  11. 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 [Select]
  1. $table = '<table>';
  2. foreach($recordset as $row) {
  3.   if ($row['SubId'] != $test) continue;
  4.   $table .= '<tr>';
  5.   $table .= '<tr>'. $row['SubId'] .'</td>';
  6.   $table .= '<td>'. $row['CatId'] .'</td>';
  7.   $table .= '<td>'. $row['SUbName'] .'</td>';
  8.   $table .= '</tr>';
  9. }
  10. $table .= '</table>';
  11.  
  12. print $table;

But it is really the best if you make the query as specific as possible.

kevstero

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 16
    • View Profile
    • Donate to Member
Re: Use a foreach loop maybe?
« Reply #4 on: June 28, 2008, 08:30 AM »
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 [Select]
  1. <?php require_once('Connections/antiques.php');
  2. $Test = $_POST['select2'];
  3. ?>
  4. <?php
  5. if (!function_exists("GetSQLValueString")) {
  6. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  7. {
  8.   $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. mysql_select_db($database_antiques, $antiques);
  35. $query_Recordset1 = "SELECT * FROM itemsubcategory";
  36. $Recordset1 = mysql_query($query_Recordset1, $antiques) or die(mysql_error());
  37. $row_Recordset1 = mysql_fetch_assoc($Recordset1);
  38. $totalRows_Recordset1 = mysql_num_rows($Recordset1);
  39. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  40. <html xmlns="http://www.w3.org/1999/xhtml">
  41. <head>
  42. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  43. <title>Untitled Document</title>
  44. </head>
  45.  
  46. <body>
  47. <table border="1" cellpadding="0" cellspacing="0">
  48.   <tr>
  49.     <td>SubId</td>
  50.     <td>CatId</td>
  51.     <td>SubName</td>
  52.   </tr>
  53.   <?php      
  54.            do { if ($row_Recordset1['SubId'] != $test) continue;
  55.   ?>
  56.     <tr>
  57.       <td><?php echo $row_Recordset1['SubId']; ?></td>
  58.       <td><?php echo $row_Recordset1['CatId']; ?></td>
  59.       <td><?php echo $row_Recordset1['SubName']; ?></td>
  60.     </tr>
  61.     <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
  62. </table>
  63. </body>
  64. </html>
  65. <?php
  66. mysql_free_result($Recordset1);
  67. ?>

kevstero

  • Participant
  • Joined in 2008
  • *
  • default avatar
  • Posts: 16
    • View Profile
    • Donate to Member
Re: Use a foreach loop maybe?
« Reply #5 on: June 28, 2008, 10:23 PM »
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,914
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: Use a foreach loop maybe?
« Reply #6 on: June 28, 2008, 11:11 PM »
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 }