Solved

arrays

Posted on 2013-05-21
14
155 Views
Last Modified: 2013-05-21
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
Comment
Question by:doctorbill
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 82

Assisted Solution

by:hielo
hielo earned 500 total points
ID: 39185417
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
 

Author Comment

by:doctorbill
ID: 39185443
Sorry - no good
Just gives me two columns with duplicate information in each row down the 2 columns
0
 

Author Comment

by:doctorbill
ID: 39185473
Same problem with second script as well
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 500 total points
ID: 39185569
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39185576
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
 
LVL 33

Expert Comment

by:Slick812
ID: 39185654
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
 

Author Comment

by:doctorbill
ID: 39185665
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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39185667
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
 

Author Comment

by:doctorbill
ID: 39185679
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
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 39185720
>>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
 

Author Comment

by:doctorbill
ID: 39185758
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
 
LVL 82

Expert Comment

by:hielo
ID: 39185785
>>($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
 

Author Comment

by:doctorbill
ID: 39185800
Absolutely fantastic - so simple compared to my initial effort.
Thanks again - this code taught me so much
0
 

Author Closing Comment

by:doctorbill
ID: 39185812
Thanks very much to all contributors
Solution to problem
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now