Hi Im trying to get mysql_query result with foreach set up and i am just planning what i am going to do but I am stuck with adding foreach into my code so far.

Basically F field has several different options which are duplicated lots of times, so i need the F field in the WHERE F = to be foreach different option in that field. (HOpe I have explained OK)

If someone could help it would be great Thanks

<?php
$con = mysql_connect("localhost","X","X");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbname", $con);

$result = mysql_query("SELECT `A`, `B`, `C`, `D`, `E`, `F`, `G`, `H` FROM TABLE WHERE `F` = 'blar' ORDER BY `H`+0 DESC LIMIT 10") or trigger_error(mysql_error().$sql);

echo "<table cellspacing='0' cellpadding='0' class='altrowstable' id='alternatecolor' width='100%'>


                                   <tr>  
                   <th>A</th>
                                        <th>B</th>
                                        <th>C</th>
                                        <th>D</th>
                                        <th>E</th>
                                        <th>F</th>
                                        <th>G</th>
                                    </tr>";
$n = 1;
while($row = mysql_fetch_array($result))
  {
echo"<div id='tab_'.sprintf("%.0f",$n++).'' class='show parent'  style='background-position:99.8% center; background-size: auto 100%; border-radius:0 0 3px 0; -moz-border-radius:0 0 3px 0; -webkit-border-radius:0 0 3px 0; -o-border-radius:0 0 3px 0; display: block;'>
                <div class='col-3'>

                    <div class='table_view'>          
<div class='table_info_head'>
<div class='info_table_outer'>
              <div class="table_info'>

  echo "<tr>";       

echo '<td>' . $row['A'] . ' with other info></td>';                           
echo '<td>' . $row['B'] . ' with other info></td>';
echo '<td>' . $row['C'] . ' with other info></td>';
echo '<td>' . $row['D'] . ' with other info></td>';
echo '<td>' . $row['E'] . ' with other info></td>';
echo '<td>' . $row['F'] . ' with other info></td>';
echo '<td>' . $row['G'] . ' with other info></td>';
  echo "</tr>";
  }
echo "</table>";

echo "</div>
            </div>
</div>
                    </div>
            </div>
</div>";

Recommended Answers

Show some sample data, perhaps it will make things clearer.

Jump to Post

Is this what you get now? If so, what should it look like?

Jump to Post

All 8 Replies

Show some sample data, perhaps it will make things clearer.

OK thanks, I have given made up examples rather than get details from the database.

A B C D E F G H
1 2 3 4 5 Billy 6 1
1 2 3 4 5 Bob 6 2
1 2 3 4 5 Billy 6 3
1 2 3 4 5 Bob 6 4
1 2 3 4 5 Sue 6 5
1 2 3 4 5 Sue 6 6
1 2 3 4 5 Simon 6 7
1 2 3 4 5 Billy 6 8
1 2 3 4 5 Sue 6 9
1 2 3 4 5 Bob 6 10
1 2 3 4 5 Simon 6 11

Is this what you get now? If so, what should it look like?

Hi No I am just planning so I get nothing, but I know that info is correct, but when I complete it without a foreach I will have to do several queries on a big database rather than 1 with a foreach.

For example

<?php
$con = mysql_connect("localhost","X","X");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT `A`, `B`, `C`, `D`, `E`, `F`, `G`, `H` FROM TABLE WHERE `F` = 'billy' ORDER BY `H`+0 DESC LIMIT 10") or trigger_error(mysql_error().$sql);
echo "<table cellspacing='0' cellpadding='0' class='altrowstable' id='alternatecolor' width='100%'>
                                   <tr>  
                   <th>A</th>
                                        <th>B</th>
                                        <th>C</th>
                                        <th>D</th>
                                        <th>E</th>
                                        <th>F</th>
                                        <th>G</th>
                                    </tr>";
$n = 1;
while($row = mysql_fetch_array($result))
  {
echo"<div id='tab_'.sprintf("%.0f",$n++).'' class='show parent'  style='background-position:99.8% center; background-size: auto 100%; border-radius:0 0 3px 0; -moz-border-radius:0 0 3px 0; -webkit-border-radius:0 0 3px 0; -o-border-radius:0 0 3px 0; display: block;'>
                <div class='col-3'>
                    <div class='table_view'>          
<div class='table_info_head'>
<div class='info_table_outer'>
              <div class="table_info'>
  echo "<tr>";       
echo '<td>' . $row['A'] . ' with other info></td>';                           
echo '<td>' . $row['B'] . ' with other info></td>';
echo '<td>' . $row['C'] . ' with other info></td>';
echo '<td>' . $row['D'] . ' with other info></td>';
echo '<td>' . $row['E'] . ' with other info></td>';
echo '<td>' . $row['F'] . ' with other info></td>';
echo '<td>' . $row['G'] . ' with other info></td>';
  echo "</tr>";
  }
echo "</table>";
echo "</div>
            </div>
</div>
                    </div>
            </div>
</div>";



$result = mysql_query("SELECT `A`, `B`, `C`, `D`, `E`, `F`, `G`, `H` FROM TABLE WHERE `F` = 'simon' ORDER BY `H`+0 DESC LIMIT 10") or trigger_error(mysql_error().$sql);
echo "<table cellspacing='0' cellpadding='0' class='altrowstable' id='alternatecolor' width='100%'>
                                   <tr>  
                   <th>A</th>
                                        <th>B</th>
                                        <th>C</th>
                                        <th>D</th>
                                        <th>E</th>
                                        <th>F</th>
                                        <th>G</th>
                                    </tr>";
$n = 1;
while($row = mysql_fetch_array($result))
  {
echo"<div id='tab_'.sprintf("%.0f",$n++).'' class='show parent'  style='background-position:99.8% center; background-size: auto 100%; border-radius:0 0 3px 0; -moz-border-radius:0 0 3px 0; -webkit-border-radius:0 0 3px 0; -o-border-radius:0 0 3px 0; display: block;'>
                <div class='col-3'>
                    <div class='table_view'>          
<div class='table_info_head'>
<div class='info_table_outer'>
              <div class="table_info'>
  echo "<tr>";       
echo '<td>' . $row['A'] . ' with other info></td>';                           
echo '<td>' . $row['B'] . ' with other info></td>';
echo '<td>' . $row['C'] . ' with other info></td>';
echo '<td>' . $row['D'] . ' with other info></td>';
echo '<td>' . $row['E'] . ' with other info></td>';
echo '<td>' . $row['F'] . ' with other info></td>';
echo '<td>' . $row['G'] . ' with other info></td>';
  echo "</tr>";
  }
echo "</table>";
echo "</div>
            </div>
</div>
                    </div>
            </div>
</div>";

$result = mysql_query("SELECT `A`, `B`, `C`, `D`, `E`, `F`, `G`, `H` FROM TABLE WHERE `F` = 'sue' ORDER BY `H`+0 DESC LIMIT 10") or trigger_error(mysql_error().$sql);
echo "<table cellspacing='0' cellpadding='0' class='altrowstable' id='alternatecolor' width='100%'>
                                   <tr>  
                   <th>A</th>
                                        <th>B</th>
                                        <th>C</th>
                                        <th>D</th>
                                        <th>E</th>
                                        <th>F</th>
                                        <th>G</th>
                                    </tr>";
$n = 1;
while($row = mysql_fetch_array($result))
  {
echo"<div id='tab_'.sprintf("%.0f",$n++).'' class='show parent'  style='background-position:99.8% center; background-size: auto 100%; border-radius:0 0 3px 0; -moz-border-radius:0 0 3px 0; -webkit-border-radius:0 0 3px 0; -o-border-radius:0 0 3px 0; display: block;'>
                <div class='col-3'>
                    <div class='table_view'>          
<div class='table_info_head'>
<div class='info_table_outer'>
              <div class="table_info'>
  echo "<tr>";       
echo '<td>' . $row['A'] . ' with other info></td>';                           
echo '<td>' . $row['B'] . ' with other info></td>';
echo '<td>' . $row['C'] . ' with other info></td>';
echo '<td>' . $row['D'] . ' with other info></td>';
echo '<td>' . $row['E'] . ' with other info></td>';
echo '<td>' . $row['F'] . ' with other info></td>';
echo '<td>' . $row['G'] . ' with other info></td>';
  echo "</tr>";
  }
echo "</table>";
echo "</div>
            </div>
</div>
                    </div>
            </div>
</div>";

etc etc etc etc

Oh, you want to select multiple users at once? Something like:

SELECT * FROM `TABLE` WHERE `F` = 'sue' OR `F` = 'simon'

Hi

Thanks for your reply but NO. The first code shows I just want to echo out that part but then echo out the same part several times for each different name in F

Hope the clears it up
Thanks

So, get all unique usernames and the loop the query for each one?

$users = mysql_query('SELECT DISTINCT `F` FROM `table`');
while ($user = mysql_fetch_assoc($users))
{
    $result = mysql_query("SELECT ... WHERE `F` = {$user['F']}");
    while ($row = mysql_fetch_assoc($result))
    {
        // process
    }
}

OK thanks for your help will try that

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.