Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Arrays

Post by pinehead18 »

Ok, wow i've just started learning arrays. I found myself in a bind at work were i really need to write this script.

What i need to do and i need to know if it is possible and perhaps maby a little insite on how to accomplish these tasks...

1.) Lets say i have 3 words in an array. Seperated by . the words are one two and three. Is it possible to take the word two out and change it to the word six. While leaving it in the same order in the array. The same order would be "one six three" the wrong order would be "one three six"

2.) I need to write a script that displays all the words in an array. Basically a loop. How might i go about this.

BTW i didn't just come running here to ask hour. I've been reading about arrays for the past couple hours trying to figure something out. I even got a notepad and pen trying to figure something up in my head.
I'm sure it is something easy i just over looked.

Thank you for any help you can provide me.

- Anthony
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Please correct me if I'm wrong but from reading your post it looks as though you are thinking about arrays in the wrong way.

For example a basic array could be created like this...

Code: Select all

<?php

$fruit[0] = "Apple";
$fruit[1] = "Banana";
$fruit[2] = "Orange";

?>
...to change any of the array values you could do this...

Code: Select all

<?php

// Change Banana to Lime
$fruit[1] = "Lime";

?>
...to display of all the array values using a loop you might use this...

Code: Select all

<?php

foreach($fruit as $value)
{
    echo $value."<br />";
}

?>
You've probably seen this already but having a look at the PHP manual might give you some more ideas. [php_man]array[/php_man]
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

That makes a tid bit more since. Maby you could helpe me out here thought
<?php

foreach($fruit as $value)
{
echo $value."<br />";
}

?>

That wa your code. What does $value do? What is it? That is what is kinda getting me. I don't know why that is their.

Also basically what i'm doing. Is i have a photo album for my family. It allows 4 pictures and i'm storing the urls in arrays. in one sql field.

So i just needed to know how to update specific vars in an array but i think you explained that to me.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Check-out the manual for [php_man]foreach[/php_man] but it's just a fancy while() or for() really.

You could just do the following to get the same result....

Code: Select all

<?php

for($i=0; $i<count($fruit); $i++)
{
    $value = $fruit[$i];
    echo $value."<br />";
}

?>
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

OK, so basically i wrote something similar to an example on the manual

$a = array ("default.gif", "default.gif", "default.gif", "default.gif");

foreach ($a as $v) {
print "Current value of \$a: $v.\n";
}


However, what i need to do is i need to make each one of those defaults into a var. A var i can change and stuff.

Is it liek this..

$a[1];
$a[2];
and it will give me my info?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Yes, but remember that arrays are zero-based. Heres an example:

Code: Select all

<?php
$a = array ("1.gif", "2.gif", "3.gif", "4.gif");
echo $a[0]; // result: 1.gif
echo $a[1]; // result: 2.gif
?>
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Ok, i have in my db field the following contents.
default.gif, default.gif, default.gif.

What i want to do is i wanted to create a loop that will display these pictures. So i did the following code.

$sql = "SELECT * FROM users where user='$name'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);

$arraypic = $row['pics'];
$pics = array($arraypic, ",");

foreach ($pics as $picid) {
print "Print html for $picid</p>";
}

yet it does not print the defaults in the db.. Any ideas?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

This might work...

Code: Select all

<?php

$sql = "SELECT * FROM users where user='$name'"; 
$result = mysql_query($sql) or die(mysql_error()); 

while($arr = mysql_fetch_array($result))
{
    $pics[] = $arr["pics"];
}

foreach($pics as $picid)
{
    echo "Print HTML for {$picid}<br />";
}

?>
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

Wow, thank you again so much for your help. Just one quick problem.

$sql = "SELECT * FROM users where user='$name'";
$result = mysql_query($sql) or die(mysql_error());

while($arr = mysql_fetch_array($result))
{
$pics[] = $arr["add_picts"];
}

foreach($pics as $picid)
{
echo "Print HTML for {$picid}</p>";
echo "</p>";
}


Thats the code.. it does not </p> at the end of default.php instead it just does this

Print HTML for default.gif,default.gif,default.gif,default.gif

I seperated the default.gif's in the db feild with ,'s

THank you
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It will be adding a </p>, it's just there's no <p> to go with it :o
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

well i changed the </p> to a <br> yet, it still does not work.

Don't i need to like explode these vars from the array and put them back into the array or something?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

pinehead18 wrote:well i changed the </p> to a <br> yet, it still does not work.

Don't i need to like explode these vars from the array and put them back into the array or something?
No you shouldn't need to do that. Run the code again and have a look at the HTML source-code once the page has loaded. You should get some idea of why it's not being displayed correctly (look for missing tags etc).
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

h, didn't see the " seperated the default.gif's in the db feild with ,'s " bit ;)

Maybe..
foreach($pics as $picid)
{
echo 'Print HTML for '.join('<br />', explode(',', $picid)).'<br />";
}

or you could str_replace(',','<br />', $picid) instead if you're sure the ,'s only appear as separators.
pinehead18
Forum Contributor
Posts: 329
Joined: Thu Jul 31, 2003 9:20 pm

Post by pinehead18 »

HEh, yeah, i've even tried some things on my own. Still am unable to get it to seperate it from one line.

For some reason arrays are just hard for me :(
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Hmm..it should Just Work[TM] :o
Maybe post the code you have now and the output it produces..and the output you actually want to get.
Post Reply