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 329,043 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 3,544 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: 134 | Replies: 10 | Solved
Reply
Join Date: Dec 2007
Posts: 140
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX OmniX is offline Offline
Junior Poster

Initialization of an Array

  #1  
3 Days Ago
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 : 3 Days Ago at 4:40 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 160
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Initialization of an Array

  #2  
3 Days Ago
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: 140
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX OmniX is offline Offline
Junior Poster

Re: Initialization of an Array

  #3  
3 Days Ago
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: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 160
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Initialization of an Array

  #4  
3 Days Ago
  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: 140
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX OmniX is offline Offline
Junior Poster

Re: Initialization of an Array

  #5  
2 Days Ago
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: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 160
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Initialization of an Array

  #6  
2 Days Ago
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: 140
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX OmniX is offline Offline
Junior Poster

Re: Initialization of an Array

  #7  
2 Days Ago
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 : 2 Days Ago at 7:25 am.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 160
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Initialization of an Array

  #8  
2 Days Ago
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: 140
Reputation: OmniX is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
OmniX OmniX is offline Offline
Junior Poster

Re: Initialization of an Array

  #9  
2 Days Ago
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 : 2 Days Ago at 8:22 am.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 2,468
Reputation: nav33n will become famous soon enough nav33n will become famous soon enough 
Rep Power: 6
Solved Threads: 160
nav33n's Avatar
nav33n nav33n is offline Offline
Nearly a Posting Maven

Re: Initialization of an Array

  #10  
2 Days Ago
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.

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

 

DaniWeb Marketplace (Sponsored Links)
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

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