Forum Moderators: coopster

Message Too Old, No Replies

Need to add items to php array

add items to php array

         

varunkrish

1:48 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



i need to add items to a php array..

i tried many things but not working..

i want to add a maximum of 10 values to a array called

poems

users must be able to read many poems side by side...

so i need a array to store the database "id" of the poems for retrieval

$p = array();

i want to store something in the array like

$p = array("1","2","5","9");

so that the statement

foreach ($p as $value) {
echo "$value<br />\n";
}

will print

1
2
5
9

please help me add elements to the array using php or Javascript

tomda

2:06 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try array_push($my_array, "banana", "orange")

dreamcatcher

2:11 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or:

$p = array();

$query = mysql_query("SELECT * FROM table WHERE something = something");

while ($row = mysql_fetch_assoc($query))
{
$p[] = $row['id'];
}

dc

varunkrish

2:20 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



thanks for ur replies...

but i need to use the array in my sql query

i need to select only the rows from the mysql table where id is in the array $p

dreamcatcher

4:33 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$p = array(1,2,5,9);

$query = mysql_query("SELECT * FROM table WHERE id IN ($p)");

dc

varunkrish

12:08 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



i am passing the array from a form

like this
<form name="form1" method="get" action="post.php">
<input name="p[]"/>
<br>
<input name="p[]"/>
<br>
<input name="p[]" />
<br>
<input name="p[]" />
<input type="submit">
</form>

so the " in " in sql query is not working properly

varunkrish

12:10 pm on Jun 29, 2005 (gmt 0)

10+ Year Member



i tried this logic.. is it correct to use a "OR" to select non successive rows in a table

$sql ="SELECT * FROM `table` WHERE ";
foreach ($p as $value) {
if(isset($fla)){
$sql.=" OR ";
}
$sql.="id=$value";
$fla=1;
}
$sql_result = mysql_query($sql);