Link to home
Create AccountLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

asked on

arrays

The attached piece of code is working in that the data from a database is returned to the page in 2 columns starting from top left to bottom right

Everything is working really well with just one exception:
I need ALL the data from the array to be represented in stead of the array records being read from the second array record  onwards
I can see why this is happening but I just cannot resolve the issue

Help please

ps I KNOW the code is not perfect but it is working well for me except for this one issue
code.txt
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of doctorbill

ASKER

Sorry - no good
Just gives me two columns with duplicate information in each row down the 2 columns
Same problem with second script as well
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
What is the purpose of this segment?

$i = "0";
$j = "0";
$k = "0";
$m = "0";
$n = "0";

Original code posted here:
<?php
$aryImg = array();
while ($row_thumbs = mysql_fetch_assoc($thumbs)) {
    $aryImg[ ] = $row_thumbs['art_thumb'];
	$aryTitle[ ] = $row_thumbs['art_title'];
	$aryName[ ] = $row_thumbs['art_Name'];
	$aryDesc[ ] = $row_thumbs['art_desc'];
	$aryLink[ ] = $row_thumbs['art_link1'];
    }

//$end = count($aryImg);
$end = mysql_num_rows($thumbs);
$i = "0";
$j = "0";
$k = "0";
$m = "0";
$n = "0";

echo '<Table width="">';
do { 

?>

   <tr valign="top"><td valign="top"><div class="artist_div_width" align="left" ><span class="body_text_large_brickred"><?php echo $aryName[$j++]; ?></span><br /><div class="artists_thumbnails"><a href="artistinfo.php?artist=<?php echo $aryName[$m++]; ?>" target="_parent"><img src="<?php echo $aryImg[$i++]; ?>" width="350"></a></div></div></td>
<div class="artist_div_width" align="left"><td valign="top"><span class="body_text_large_brickred"><?php echo $aryName[$j++]; ?></span><br /><div class="artists_thumbnails"><a href="artistinfo.php?artist=<?php echo $aryName[$m++]; ?>" target="_parent"><img src="<?php echo $aryImg[$i++]; ?>" width="350"></a></div></div>
</td></tr>
        
<?php } while ($i <= $end); 
echo '</Table>';
  ?>

Open in new window

greetings   doctorbill, , looked at your code, and it seems to be in order to show all of the records, you say = "records being read from the second array record  onwards", You seem to think that a STRING is the same as an Integer as you have -
$j = "0";
    shouldn't that be -
$j = 0;

This just may fix your problem?

just noticed something else, you have a <DIV> outside of a <TD> as -
<div class="artist_div_width" align="left"><td valign="top">

and that can not be correct code can it ? ?
The script above posted by hielo is very very close. The results are still missing one record though - the first one of the array
When I recast the code to make it more readable I see that the nesting of the div and td tags seems inconsistent.  Not sure exactly what the problem is, but it might make sense to check that and see if the HTML is rendering the way you want.

Also, it appears that $k and $n seems to have been omitted.  You might want to check that part, too.
<?php
$aryImg = array();
while ($row_thumbs = mysql_fetch_assoc($thumbs)) {
    $aryImg[ ] = $row_thumbs['art_thumb'];
	$aryTitle[ ] = $row_thumbs['art_title'];
	$aryName[ ] = $row_thumbs['art_Name'];
	$aryDesc[ ] = $row_thumbs['art_desc'];
	$aryLink[ ] = $row_thumbs['art_link1'];
    }

//$end = count($aryImg);
$end = mysql_num_rows($thumbs);
$i = "0";
$j = "0";
$k = "0";
$m = "0";
$n = "0";

echo '<Table width="">';
do { 

?>

<tr valign="top">
<td valign="top">
<div class="artist_div_width" align="left" >
  <span class="body_text_large_brickred">
    <?php echo $aryName[$j++]; ?>
  </span>
  <br />
  <div class="artists_thumbnails">
    <a href="artistinfo.php?artist=<?php echo $aryName[$m++]; ?>" target="_parent">
      <img src="<?php echo $aryImg[$i++]; ?>" width="350">
    </a>
  </div>
</div>
</td>
<div class="artist_div_width" align="left">
  <td valign="top">
  <span class="body_text_large_brickred">
    <?php echo $aryName[$j++]; ?>
  </span>
  <br />
  <div class="artists_thumbnails">
    <a href="artistinfo.php?artist=<?php echo $aryName[$m++]; ?>" target="_parent">
      <img src="<?php echo $aryImg[$i++]; ?>" width="350">
    </a>
  </div>
</div>
</td>
</tr>
        
<?php } while ($i <= $end); 
echo '</Table>';
?>

Open in new window

This code (hielo) is almost there:
It is just missing the FIRST record

<?php
if( mysql_num_rows($thumbs) )
{
      echo '<table width=""><tr valign="top">';
      $i=0;
      while ($row_thumbs = mysql_fetch_assoc($thumbs))
      {
      ?>
            <td valign="top">
                  <div class="artist_div_width" align="left" >
                        <span class="body_text_large_brickred"><?php echo $row_thumbs['art_Name']; ?></span>
                        <br />
                        <div class="artists_thumbnails">
                              <a href="artistinfo.php?artist=<?php echo $row_thumbs['art_Name']; ?>" target="_parent"><img src="<?php echo $row_thumbs['art_thumb']; ?>" width="350"></a>
                        </div>
                  </div>
            </td>
      <?php
            $i++;
            if(  0==($i % 2) )
            {
                  echo '</tr><tr valign="top">';
            }
      }
      echo '</tr></table>';
}
?>
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
hielo - PERFECT - -THANKS a MILLION

In order for my learning curve , can you explain this line a bit more please:
--------------------------
if(  0==($i % 2) )
>>($i % 2)
That is just doing modular arithmetic.  In other words, take the value of $i and divide it by 2, discard the quotient, but keep the remainder.  The remainder IS the result of ($i % 2).

That section of the code just makes sure that you close the previous row and start a new one whenever you have echoed two rows.  If you wanted to use three rows, then you would just need to use ($i % 3).  Simpler than your original arrays don't you think?

Hielo
Absolutely fantastic - so simple compared to my initial effort.
Thanks again - this code taught me so much
Thanks very much to all contributors
Solution to problem