lordphate Posted June 21, 2007 Share Posted June 21, 2007 Hi everyone its your friendly n00b here hehe... Okay so i've been learning and learning and learning and i came to a spot i'm not too sure about. Arrays and strings. In my php document that i'm editing to make work for my site it pulls "answers" from a string that looks something like this "|path/to/photos/1.jpg|path/to/photos2.jpg|" etc. I found a way to make the script put the |'s in front of the variable with no problems, though now when i try to make another "answer" (but want to keep the original there) it just replaces the original.. Any ideas on how i could accomplish this? This is what i have so far <?php $sql_query = "SELECT photo_small FROM photos WHERE pho_id=".$pho_id; $photo_small = $GLOBALS["DB"]->single($sql_query); $sql_query = "UPDATE contest SET answers='".$photo_small."|'"; $GLOBALS["DB"]->execute($sql_query); ?> There will be eventually hundreds of "answers" Thanks in advance!!! On another note(sorry) function contest_extract_answers($answers_code){ $answers = explode("|",trim($answers_code,"|")); array_unshift($answers,""); unset($answers[0]); $out =''; foreach($answers as $v) {$out .= "<img src='./photos/".$v."'>";}; //return $answers; return $out; }//contest_extract_answers This is how their extracted Link to comment https://forums.phpfreaks.com/topic/56580-arrays-and-strings-help/ Share on other sites More sharing options...
chigley Posted June 21, 2007 Share Posted June 21, 2007 $variable = "foo"; echo $variable; // foo $variable .= "bar"; // note the . before the = echo $variable; // foobar Use .= to add to existing variable. Link to comment https://forums.phpfreaks.com/topic/56580-arrays-and-strings-help/#findComment-279431 Share on other sites More sharing options...
lordphate Posted June 21, 2007 Author Share Posted June 21, 2007 Okay I tried $sql_query = "SELECT photo_small FROM photos WHERE pho_id=".$pho_id; $photo_small .= $GLOBALS["DB"]->single($sql_query); but that doesn't work still replaces. I dont know if i explained what it is i'm doing This will be for a voting contest script. People will have a link under their photos saying "Add to voting" and thats when it's supposed to add it to the existing answers Link to comment https://forums.phpfreaks.com/topic/56580-arrays-and-strings-help/#findComment-279485 Share on other sites More sharing options...
lordphate Posted June 21, 2007 Author Share Posted June 21, 2007 Arg, i just found out i need to use implode and explode. I'm looking it up on php.net but it's looking awfully confusing to me lol Link to comment https://forums.phpfreaks.com/topic/56580-arrays-and-strings-help/#findComment-279527 Share on other sites More sharing options...
trecool999 Posted June 21, 2007 Share Posted June 21, 2007 Arg, i just found out i need to use implode and explode. I'm looking it up on php.net but it's looking awfully confusing to me lol $foo = 'Var1|^|Var2|^|Var3'; $split = explode('|^|', $foo); echo $split[0] . '<br />' . $split[1] . '<br />' . $split[2]; //Would echo 'Var1<br />Var2<br />Var3<br />' Not that confusing really, it seperates the string with the delimiters provided and turns each value into an array for use. Link to comment https://forums.phpfreaks.com/topic/56580-arrays-and-strings-help/#findComment-279532 Share on other sites More sharing options...
lordphate Posted June 21, 2007 Author Share Posted June 21, 2007 Trecool Yeah THATS not confusing at all But even with that, i need it to add the $photo_small to the contest table seperated by | Displaying the array isn't the problem its storing and deleting certain points in the array is the issue Link to comment https://forums.phpfreaks.com/topic/56580-arrays-and-strings-help/#findComment-279565 Share on other sites More sharing options...
lordphate Posted June 21, 2007 Author Share Posted June 21, 2007 <?php // array containing data $array = array( "name" => "John", "surname" => "Doe", "email" => "[email protected]" ); // build query... $sql = "INSERT INTO table"; // implode keys of $array... $sql .= " (`".implode("`, `", array_keys($array))."`)"; // implode values of $array... $sql .= " VALUES ('".implode("', '", $array)."') "; // execute query... $result = mysql_query($sql) or die(mysql_error()); ?> I found this on php.net and i'm trying to figure out how to UPDATE instead of inserting a new row. what would i need to do Link to comment https://forums.phpfreaks.com/topic/56580-arrays-and-strings-help/#findComment-279604 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.