Page 1 of 2
Arrays
Posted: Sat Jan 17, 2004 7:18 pm
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
Posted: Sat Jan 17, 2004 9:07 pm
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]
Posted: Sat Jan 17, 2004 9:15 pm
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.
Posted: Sat Jan 17, 2004 9:21 pm
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 />";
}
?>
Posted: Sat Jan 17, 2004 9:30 pm
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?
Posted: Sat Jan 17, 2004 9:38 pm
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
?>
Posted: Sat Jan 17, 2004 10:44 pm
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?
Posted: Sun Jan 18, 2004 7:34 am
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 />";
}
?>
Posted: Sun Jan 18, 2004 10:40 am
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
Posted: Sun Jan 18, 2004 10:43 am
by markl999
It will be adding a </p>, it's just there's no <p> to go with it

Posted: Sun Jan 18, 2004 10:52 am
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?
Posted: Sun Jan 18, 2004 10:58 am
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).
Posted: Sun Jan 18, 2004 11:00 am
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.
Posted: Sun Jan 18, 2004 11:20 am
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

Posted: Sun Jan 18, 2004 11:26 am
by markl999
Hmm..it should Just Work[TM]

Maybe post the code you have now and the output it produces..and the output you actually want to get.