简体   繁体   中英

Multidimensional Arrays two loops

MSSQL query link is this; click here for picture

and my output shows three different pictures for the same product as shown below. What I want is, if the product is the same, keep just one picture and then get the colors and sizes for that product.

Means;

My output ıs the picture below,

click here for picture

as you see there are three product in the picture but they are the same product with different colors and sizes, instead of seeing the same product every time, I want my output like in the picture below.

 
<table width="376" cellspacing="0"  class="stats" width:100%> 
<tr> 
<td colspan="9" align="center"><?php echo $secim ?></td> 
</tr> 
<?php 
while(odbc_fetch_into($sql_result, &$row)) { 
$unit1 = floor($row[3]); 
$unit2 = floor($row[4]); 
$unit3 = floor($row[5]); 
$unit4 = floor($row[6]); 
$unit5 = floor($row[7]); 
?> 
<tr> 
  <td colspan="2" align="left" valign="top"><?php echo"$row[0]";?></td> 
  <td>36</td> 
  <td>38</td> 
  <td>40</td> 
  <td>42</td> 
  <td>44</td> 
</tr> 
<tr>       
<td width="114" align="right" valign="top">
<img src= <?php echo"images/Resize/$row[2]"?>></td> 
 <td width="25" valign="top"><?php echo"$row[1]";?></td> 
 <td width="25"valign="top"><?php echo"$unit1";?></td> 
 <td width="25"valign="top"><?php echo"$unit2";?></td> 
 <td width="25"valign="top"><?php echo"$unit3";?></td> 
 <td width="25"valign="top"><?php echo"$unit4";?></td> 
 <td width="25"valign="top"><?php echo"$unit5";?></td> 
 </tr> 
 <?php }  }?> 
 <?php  
 odbc_free_result($sql_result); 
 odbc_close($connection); 
 ?> 
 </table>

                       
                    

I think this is what you are looking for, http://jsfiddle.net/sv8ZS/

while(odbc_fetch_into($sql_result, &$row)) { 
$unit1 = floor($row[3]); 
$unit2 = floor($row[4]); 
$unit3 = floor($row[5]); 
$unit4 = floor($row[6]); 
$unit5 = floor($row[7]); 
?> 
//you can check with the index to see if its a first row or not
//This will avoid printing the same header for each row.
if (this-is-the-first row) {
  <tr> 
    <td colspan="2" align="left" valign="top"><?php echo"$row[0]";?></td> 
    <td>36</td> 
    <td>38</td> 
    <td>40</td> 
    <td>42</td> 
    <td>44</td> 
  </tr> 
} 
//above if ends
<tr>  
//If its not a first row then do not show the image again and merge all the next rows
if (this-is-the-first-row) {     
   <td width="114" align="right" valign="top" rowspan='3'>
    <img src= <?php echo"images/Resize/$row[2]"?>
   </td>  
 }
 //above if ends 
 <td width="25" valign="top"><?php echo"$row[1]";?></td> 
 <td width="25"valign="top"><?php echo"$unit1";?></td> 
 <td width="25"valign="top"><?php echo"$unit2";?></td> 
 <td width="25"valign="top"><?php echo"$unit3";?></td> 
 <td width="25"valign="top"><?php echo"$unit4";?></td> 
 <td width="25"valign="top"><?php echo"$unit5";?></td> 
 </tr> 
 <?php }  }?> 
enter code here
<?php
echo "<table border='1'>
<tr>
<th>Model</th>
<th>Color</th>
<th>Unit</th>
</tr>";
?>
<?php
while($row =odbc_fetch_array($result)){
//if $row['sAciklama']=$row['sAciklama'] bla bla ???
if (this-is-the-first row) {
echo "<tr>";
echo "<td>" . $row['sAciklama'] . "</td>"; //Model First Element 
}
if (this-is-the-first-row) {  
echo "<td>" . $row['sRenkAdi'] . "</td>"; //color
echo "<td>" . $row['Kalan'] . "</td>";    //unit
echo "</tr>";
}}
echo "</table>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM