Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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
0
doctorbill
Asked:
doctorbill
  • 7
  • 4
  • 2
  • +1
3 Solutions
 
hieloCommented:
try:
<?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;
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[$i]; ?></span><br />
			<div class="artists_thumbnails"><a href="artistinfo.php?artist=<?php echo $aryName[$i]; ?>" 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[$i]; ?></span><br /><div class="artists_thumbnails"><a href="artistinfo.php?artist=<?php echo $aryName[$i]; ?>" 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


If you are not using $ary_Name, etc. anywhere else, then try echoing directly within the while():

<?php
if( mysql_num_rows($thumbs) )
{echo '<Table width="">';
	while ($row_thumbs = mysql_fetch_assoc($thumbs)) {
?>

   <tr valign="top">
	<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>
<div class="artist_div_width" align="left"><td valign="top"><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></tr>
        
<?php
	}
	echo '</Table>';
}

Open in new window

0
 
doctorbillTechAuthor Commented:
Sorry - no good
Just gives me two columns with duplicate information in each row down the 2 columns
0
 
doctorbillTechAuthor Commented:
Same problem with second script as well
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
hieloCommented:
Try the code below.  If the problem persists, post a link to your page.
<?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>';
}
?>

Open in new window

0
 
Ray PaseurCommented:
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

0
 
Slick812Commented:
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 ? ?
0
 
doctorbillTechAuthor Commented:
The script above posted by hielo is very very close. The results are still missing one record though - the first one of the array
0
 
Ray PaseurCommented:
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

0
 
doctorbillTechAuthor Commented:
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>';
}
?>
0
 
hieloCommented:
>>It is just missing the FIRST record
Try adding the highlighted line to my post:
<?php
if( mysql_num_rows($thumbs) )
{
      mysql_data_seek($thumbs, 0);

      echo '<table width=""><tr valign="top">';
...
}
0
 
doctorbillTechAuthor Commented:
hielo - PERFECT - -THANKS a MILLION

In order for my learning curve , can you explain this line a bit more please:
--------------------------
if(  0==($i % 2) )
0
 
hieloCommented:
>>($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
0
 
doctorbillTechAuthor Commented:
Absolutely fantastic - so simple compared to my initial effort.
Thanks again - this code taught me so much
0
 
doctorbillTechAuthor Commented:
Thanks very much to all contributors
Solution to problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now