Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

PHP array gets emptied too soon


  • Please log in to reply

#1
Metallica

Metallica

    Spyware Veteran

  • GeekU Moderator
  • 31,676 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
  • 31,676 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
  • 31,676 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