Jump to content

Arrays and Strings Help!


lordphate

Recommended Posts

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

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

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.

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.