User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,485 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,210 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 531 | Replies: 10 | Solved
Reply
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Initialization of an Array

  #1  
May 9th, 2008
I use an array for the first time in a while loop without declaring it(as php does not need it)
  1. $variable_array[] = $fetch_array['column'];

As the while loop is repeating it increases the number of the array.
Am I doing this right?

Should I have before the loop some code like:
  1. $variable_array = array();

Dont know abit confused and lost.

Thanks, Regards X
Last edited by OmniX : May 9th, 2008 at 4:40 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Initialization of an Array

  #2  
May 9th, 2008
As you have already said it yourself, you don't have to explicitly declare a variable as an array to use it like an array. You can simply use $var[] = "value"; This will treat $var as an array. But, its always a good practice to initialize a variable before using it. (But most of them don't do it )
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Initialization of an Array

  #3  
May 9th, 2008
Nav to the rescue again!

Umm nav I can declare it but I cant initialize it as I dont want to give it a value till used in the while loop, so.

What code would I use?

$var[];

?
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Initialization of an Array

  #4  
May 9th, 2008
  1. <?php
  2. $var = array( ); //declare an array
  3. $query = "select * from table";
  4. $result = mysql_query($query);
  5. while($row = mysql_fetch_array($result)) {
  6. $var[] = $row['name']; //assign a value
  7. }
  8. ?>
Note: The code works even without declaring a variable as array.
But, you can't use a normal variable to act like an array. Example,
  1. <?php
  2. $x = 100;
  3. $x[] = "10";
  4. print $x;
  5. ?>
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Initialization of an Array

  #5  
May 9th, 2008
Ya nav that what I have, but I cant seem to print the $var variable should just be print/print_r/echo $var; but none of it works...
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Initialization of an Array

  #6  
May 9th, 2008
Why ? print_r($arrayname) should work fine.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Initialization of an Array

  #7  
May 9th, 2008
Its being stupid, in short.

It wont let me declare the array before the html, I thought it was possible?

Anyways I have found another way to do it, just need a questions answered.

[code syntax="php"]
while($row = mysql_fetch_array($result)) {
//then to release after it is used so you can reuse it is?
mysql_free_result($result);
[/code]

This correct, Thanks.
Last edited by OmniX : May 9th, 2008 at 7:25 am.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Initialization of an Array

  #8  
May 9th, 2008
When you execute a query, a resource identifier will be assigned to $result. mysql_free_result will free this value.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Dec 2007
Posts: 363
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX's Avatar
OmniX OmniX is offline Offline
Posting Whiz

Re: Initialization of an Array

  #9  
May 9th, 2008
Allowing me to reuse the query again or in other words fetch it again?

Man nav php can be so annoying at times I fix one error and a new error pops up. I throw the arrays in a forloop and now more errors . Ill get back to debuging and keep you posted to see if anything works.

PS: can you run queries in a for loop? That could be a possible error? or declare it differently?
Last edited by OmniX : May 9th, 2008 at 8:22 am.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Initialization of an Array

  #10  
May 9th, 2008
I don't know whats causing the error. You can run queries in a for loop. Once you have freed the result using mysql_free_result, that resource identifier is freed. Actually, if your query isn't too complicated, you don't have to use free_result as php frees the result at the end of execution of the script. Source: http://nl3.php.net/mysql_free_result

Can you post your code here ?
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 4:44 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC