Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Assign variables to an array..

"tearing my hair out"

   
11:22 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry for the simplicity of this post. I am attempting to assign values from a previous page to an array. The page being displayed just prints "Array". If I uncomment the print line it prints the values correctly. What am I doing wrong. Please no one say "trying to program in php you raving monkey.." as it may scare me for life :-)

<?php
$IDArray =("");
// use a "for" loop to read and display array elements
for($count = 0; $count < sizeof($ID); $count++)

{
$IDArray[]=$ID[$count];
//print "'$ID[$count]', ";
}
print ("$IDArray")
?>

11:39 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Well, you don't want to print the array, just it's elements or keys. Try this:


<?
$ID=array("one", "two", "three");
for($count = 0; $count < sizeof($ID); $count++) {
$IDArray[]=$ID[$count];
}
foreach($IDArray as $key => $val) {
print("$key -- $val<br />");
}
?>

Nick

11:52 am on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Nick

Sorry I think I have not explained myself well enough. The printing side of things was to show that the variables were being passed from the previous page from multiple checkboxes all asigned a name of ID[].

What I would like to do is insert the values into an array whic will then allow me to call the variable in an SQL delete statement. Block delete really. Just having emmense problems. As you can probably tell I am fumbling arond a littlewit this.

Cheers

12:10 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Well, the above will do that just fine...

Presuming $ID exists you'll have no trouble.

Nick

3:33 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Nick

That worked and I was able to print out both the index and the value.

Do you have a suggestion on how I could insert the values into the following SQL statement though.

$sql = "DELETE FROM MMUser WHERE UserID IN (insert values from array)";

Thanks for your patience.

Richard

4:00 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



beginning of your query

$sql = "DELETE FROM MMUser WHERE UserID IN (";

//add all of your values
foreach($IDArray as $key => $val) {
$sql .= $val . ",";
}

//cut the trailing comma, (ergophobe's method, Lisa's admonishment)
$sql = substr($fields, -1);

//add the final parenthesis
$sql .= ")";

4:23 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Looks like that would work though I went a little glassy eyed looking at it, (deep into a project of my own)

Nick

7:09 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Administrator ergophobe is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




//cut the trailing comma, (ergophobe's method, Lisa's admonishment)

Naw, I just stole it from someone who stole it from someone who stole it from someone. Once someone complained to Woody Guthrie that his songs were being plagiarized by another singer and Woody said "Aw, he ain't nothing. He just steals from me, but I steal from everybody."

Tom

7:11 pm on Jul 2, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



oh well, you happened to post it and I grabbed it from there so.. ;)
11:29 am on Jul 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks to all of you who have helped me in the last few days. I got it to work with a small alteration to jatar_k's example.

For some reason though it throws an sql error. Wierd though, cos if I put the sql returned into phymyadmin it works. ??

The code is as follows

$sql = "DELETE FROM MMUser WHERE UserID IN (";

//add all of your values
foreach($IDArray as $key => $value) {
$sql .= "'$value',";
}

// remove the last comma and return
$sql = substr($sql, 0, -1);

//add the final parenthesis
$sql .= ")";

 

Featured Threads

Hot Threads This Week

Hot Threads This Month