Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

arrays

Posted on 2013-05-21
14
158 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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 109

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 34

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
 
LVL 109

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
000webhost.com default error log 1 39
Logic behind "best rated" calculation 11 30
How to fix Datetime in MySQL? 4 48
Showing multiple maps using PHP and Google Maps 8 19
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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