Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 05-04-2005, 02:52 PM Why Array?
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
Hi, can anyone tell me what Array means? basically im expecting some figures to appear from mysql database but all i get is a single word "Array", its driving me crazy. this is my code:
PHP Code:

<?php
session_start
();
?>
<html>
<head>
</head>
<body>
<?php
$user
="root"
$host="localhost";
$password="";
$connection mysql_connect($host,$user,$password)
                  or die (
"couldn't connect to server");

$db mysql_select_db("doorstop")
                  or die (
"couldn't locate the database");

$query "SELECT * FROM price WHERE glass='$_SESSION[door1glass]'";

$result=mysql_query($query)

or die (
"couldn't");


$row=mysql_fetch_array($resultMYSQL_BOTH);

{
echo
 
"$row";
}

?>
</body>
</html>
Thanks everybody.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-04-2005, 03:22 PM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
Hi again hiptobesquare, good to see you're carrying on learning PHP!

An array, in very simple terms, is a collection of variables placed into one big variable! But an array isn't simply just that. The PHP manual explains array's as being a map, which is probably a good way to explain the complexity of them.

For example, an array can simply have data (simply information that makes no sense if viewed, because it hasn't been placed with anything), so if i had a shopping list this would be perfect:
  1. Apples
  2. Banana's
  3. Bacon
  4. Cheese
  5. etc

To call these values, php assigns numbers to the values, starting from 0. Lets say our variable is called $shoppinglist. So to extract Apples, we do $shoppinglist[0]; and Bacon is $shoppinglist[2]; But what if we needed more than one pack of bacon?

You can get an ordered array, with keys which gives us order to the data!
  • [apples] 1
  • [banana] 2
  • [bacon] 2
  • [cheese] 1

To read up further you need to looke at the PHP Manual

To fix you're problem though, we need to extract the information from the array:

PHP Code:
<?php 
session_start
(); 
?> 
<html> 
<head> 
</head> 
<body> 
<?php 
$user
="root"
$host="localhost"
$password=""
$connection mysql_connect($host,$user,$password
                  or die (
"couldn't connect to server"); 

$db mysql_select_db("doorstop"
                  or die (
"couldn't locate the database"); 

$query "SELECT * FROM price WHERE glass='$_SESSION[door1glass]'"

$result mysql_query($query)
    or die(
"Query failed: " mysql_error());

while ( 
$row mysql_fetch_array($result) ) {

    echo(
'Value: ' $row[0] . '<BR>');

}


?> 
</body> 
</html>
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 05-04-2005, 04:26 PM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
Ha nice one. by changing the $row[0] to $row[1] in your version i got the number i was looking for. all i have to do now is make the size of the door the user entered earlier trigger the correct row number. Let me spend a couple of days trying to figure that for myself or ill never learn. watch this space ill probably end up asking in the end anyway.. Thankyou Leavethisplace you are proofing to be an invaluable resource in my quest for intelligence.
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 05-04-2005, 04:51 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
When you use mysql_fetch_array() you can also recall things using the databse field names - makes it a lot easier to read and understand your code later on:

Say you had a table
Code:
ID      name    age
1       Fred    45
2       John    32
3       William 12
etc...
Then after a select query you can say:

PHP Code:
$result mysql_query("select * from tablename");

$row mysql_fetch_array($result);

echo 
$row[0]."<br />";
echo 
$row[1]."<br />";
echo 
$row[2]."<br />";

echo 
$row['ID']."<br />";
echo 
$row['name']."<br />";
echo 
$row['age']."<br />"
That will result in:

Code:
1
Fred
45
1
Fred
45
Since the two methods are the same. It just makes it easier for you later when you come along to change something and have to remember what $row[14] meant the first time.....
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 05-04-2005, 08:46 PM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
Trades: 0
I personaly like to use the Name method.

You can also display all the values in the array without having to type out $row[0]-$row[100]

PHP Code:
$result mysql_query("select * from tablename"); 

while(
$row=mysql_fetch_array($result)){
echo 
"ID - $row['ID'] / NAME - $row['NAME'] / AGE - $row['AGE']<BR>";

__________________

Please login or register to view this content. Registration is FREE
- 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Old 05-05-2005, 05:48 AM
leavethisplace's Avatar
Ultra Talker

Posts: 297
Trades: 0
That will only work on mysql_fetch_array though, mysql_fetch_row only gives you numbers. But the named array is much nicer, it's just so much easier to look through code!
__________________
A lie gets halfway around the world before the truth has a chance to get its pants on. - Sir Winston Churchill

Please visit my sites:
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
leavethisplace is offline
Reply With Quote
View Public Profile
 
Old 05-05-2005, 06:36 AM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
However, as you will see below im using a variable to trigger the row number. Good to know though thanks people... Which brings me on to my next issue: This is the same page of code as above but at a later stage- Im using a loop to get my desired value from mysql the correct number of times, however this loop is no good to me unless i can change the query from
$query = "SELECT * FROM price WHERE glass='$_SESSION[door1glass]'";
to
$query = "SELECT * FROM price WHERE glass='$_SESSION[door2glass]'";
on 2nd loop and
$query = "SELECT * FROM price WHERE glass='$_SESSION[door3glass]'";
on 3rd loop and so on.

I also have to increment the echo statement just below it each time to read door 2 or door 3 respectively.
Thanks again for all your help people soon il be able to read other posts and help them out.


PHP Code:
<?php 
session_start
(); 
?> 
<html> 
<head> 
</head> 
<body> 
<?php 
$user
="root"
$host="localhost"
$password=""
$connection mysql_connect($host,$user,$password
                  or die (
"couldn't connect to server"); 

$db mysql_select_db("doorstop"
                  or die (
"couldn't locate the database"); 

if (
$_SESSION[doorsize] <= '610')
   {
   
$doorrow '1';
   }
elseif (
$_SESSION[doorsize] >= '611' and $_SESSION[doorsize] <= '762')
   {
   
$doorrow '2';
   }
elseif (
$_SESSION[doorsize] >= '763' and $_SESSION[doorsize] <= '914')
   {
   
$doorrow '3';
   }
else
   {
   
$doorrow '4';
   }

for (
$i 1$i<=$_SESSION[numdoor];$i++)


// this is the query im talking about, at the moment i get the desired number of loops but 
the same output each time://

{
$query "SELECT * FROM price WHERE glass='$_SESSION[door1glass]'"
$result mysql_query($query
    or die(
"Query failed: " mysql_error()); 
while ( 
$row mysql_fetch_array($result) ) { 

    echo(
'door 1 price: ' $row[$doorrow] . '<BR>'); 
}


?> 
</body> 
</html>


never used a loop before last night so i dont know but I get the feeling im going to have to use another set of "ifs" and create a new page for each possible outcome. ?????
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Old 05-05-2005, 08:47 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
That variable you use to trigger the field type - it can also be a string, doesn't need to be a number although it is sometimes useful.

PHP Code:
if ($_SESSION[doorsize] <= '610')
   {
   
$doorrow 'small_doors';
   } 
Is a lot more readable and works just fine.
This is really just a stylistic point and won't affect the functionality of your code. If you come back to it later it will make it easier to remember what you were thinking when you wrote it. (Or you could make it horrible so that if you are working for a company then they have to re-employ you since only you can understand your code... )
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 05-05-2005, 09:20 AM
hiptobesquare's Avatar
Extreme Talker

Posts: 186
Location: London UK
Trades: 0
ok ill remember that thankyou. I figured out the loop problem by doing this:

PHP Code:
<?php 
session_start
(); 
?> 
<html> 
<head> 
</head> 
<body> 
<?php 
$user
="root"
$host="localhost"
$password=""
$connection mysql_connect($host,$user,$password
                  or die (
"couldn't connect to server"); 

$db mysql_select_db("doorstop"
                  or die (
"couldn't locate the database"); 


include (
'doorrow.php');

if (
$_SESSION[numdoor] == '2')
{
include(
'value2door.php');
}

elseif (
$_SESSION[numdoor] == '3')
{
include(
'value3door.php');
}

elseif (
$_SESSION[numdoor] == '4')
{
include(
'value4door.php');
}

elseif (
$_SESSION[numdoor] == '5')
{
include(
'value5door.php');
}

else
{
echo 
$_SESSION[seekassist]";
}
?> 
</body> 
</html>
where doorrow.php is a program that runs the 1st if statement in my post above and value*door.php runs the mysql query from above the appropriate number of times with different values in the appropriate places. Im on to designing the site now, still havnt figured out why my full page wont show until i click refresh though:

http://www.webmaster-talk.com/showthread.php?t=28574
hiptobesquare is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Why Array?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 3.18079 seconds with 12 queries