Welcome to WebmasterWorld Guest from 54.242.224.250

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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


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 July 2, 2002 (gmt 0)

Senior Member

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

joined:Feb 4, 2002
posts:5044
votes: 0


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 July 2, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


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 July 2, 2002 (gmt 0)

Senior Member

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

joined:Feb 4, 2002
posts:5044
votes: 0


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

Presuming $ID exists you'll have no trouble.

Nick

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


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 July 2, 2002 (gmt 0)

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


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 July 2, 2002 (gmt 0)

Senior Member

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

joined:Feb 4, 2002
posts:5044
votes: 0


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 July 2, 2002 (gmt 0)

Moderator

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

joined:Apr 25, 2002
posts:8324
votes: 154



//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 July 2, 2002 (gmt 0)

Administrator

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

joined:July 24, 2001
posts:15755
votes: 0


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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Dec 6, 2001
posts:2213
votes: 0


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 .= ")";

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members