Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

PHP array gets emptied too soon


  • Please log in to reply

#1
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 33,101 posts
I am trying to transfer some rows that meet a certain condition from a MYSQL db to an array.

But the content of the array gets wiped out mostly.

.
.
//snip
.
.

$j=0;
for ( $i=1; $i<=$num_rows; $i++ ) {
		$row = mysql_fetch_array($retid); 
		if ( $category == $row["category"]) {
		   $j=$j+1;
		   $plaatjes=array(
		   "html" => array ($j => $row["html"]),
		   "kaart" => array ($j => $row["kaart"]),
		   "alt" => array ($j=> $row["ALTtekst"])
		   );
		   print_r($plaatjes);	// 26 elements with 3 keys each are in the array
		   }
		   }
$page1=($page*15)-15;
$number_pages=(floor($j/15)+1);

for ($j=$page1+1; $j<=$page1+5; $j++) {
	echo $j;
	print_r($plaatjes);		   // only Array element [26] is left, the others are gone
	echo '<BR>';
	}
.
.
.
//snip
.
.

I placed the echo and print_r commands in an attempt to find out what happens.
Probably a beginners mistake, but it's beginning to frustrate me.

Thanks for your time.
  • 0

Advertisements


#2
Michael

Michael

    Retired Staff

  • Retired Staff
  • 1,869 posts
what happens if you change
$plaatjes=array(
to
$plaatjes[i]=array(

??

Your wiping out the contents of $plaatjes each time. You need to put the [i] in so $plaatjes also acts as an array with little arrays inside it.

Edited by Michael, 07 April 2007 - 10:42 PM.

  • 0

#3
Metallica

Metallica

    Spyware Veteran

  • Topic Starter
  • GeekU Moderator
  • 33,101 posts
Thanks. That worked Michael. :whistling:

Do you have a link to a site where I can find some material to study on this subject?

I have been loooking here: http://nl2.php.net/m...types.array.php
but obviously it's over my head. :blink:
  • 0

#4
Metallica

Metallica

    Spyware Veteran

  • Topic Starter
  • GeekU Moderator
  • 33,101 posts
So I don't put anyone on the wrong track, that piece of code now looks like this:
$j=0;
for ( $i=1; $i<=$num_rows; $i++ ) {
		$row = mysql_fetch_array($retid); 
		if ( $category == $row["category"]) {
		   $j=$j+1;
		   $plaatjes[$j]=array(
		   "html" => $row["html"],
		   "kaart" => $row["kaart"],
		   "alt" => $row["ALTtekst"]
		   );
		   }
		   }

  • 0

#5
Michael

Michael

    Retired Staff

  • Retired Staff
  • 1,869 posts
and so nobody copies that code :whistling:
$j=0;
while($row = mysql_fetch_array($retid)) {
	if ( $category == $row["category"]) {
		   $plaatjes[$j]=array(
		   "html" => $row["html"],
		   "kaart" => $row["kaart"],
		   "alt" => $row["ALTtekst"]
		   );
		  j++;
	}
}

I always just used php.net. It is worth trying to understand it if you plan to keep doing bit of php in the future.
I really I never need to use any other site. I knew JavaScript (I am a Firefox extension developer) fairly well before I started doing PHP. So it was never too hard.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP