homepage Welcome to WebmasterWorld Guest from 54.211.7.174
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Assign variables to an array..
"tearing my hair out"
ukgimp




msg:1297108
 11:22 am on Jul 2, 2002 (gmt 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")
?>

 

Nick_W




msg:1297109
 11:39 am on Jul 2, 2002 (gmt 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

ukgimp




msg:1297110
 11:52 am on Jul 2, 2002 (gmt 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

Nick_W




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

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

Presuming $ID exists you'll have no trouble.

Nick

ukgimp




msg:1297112
 3:33 pm on Jul 2, 2002 (gmt 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

jatar_k




msg:1297113
 4:00 pm on Jul 2, 2002 (gmt 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 .= ")";

Nick_W




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

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

Nick

ergophobe




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


//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

jatar_k




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

oh well, you happened to post it and I grabbed it from there so.. ;)

ukgimp




msg:1297117
 11:29 am on Jul 4, 2002 (gmt 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 .= ")";

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved