0

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>";
2
Contributors
8
Replies
25
Views
4 Years
Discussion Span
Last Post by mpc123
0

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

0

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
0

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

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

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

0

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
    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.