Jump to content

looping problem -help-


webtuto

Recommended Posts

hi

i have a code that INSERT information in a database

 

 

$sql = mysql_query("INSERT INTO `docs` (`artist` ,`genre` ,`link`)VALUES ('".addslashes($this->show_caption($caption))."', 'hiphop' ,'".addslashes($this->show_href($href))."')");

 

 

 

$this->show_caption($caption) and $this->show_href($href) have at least 10 values

 

so i want every re sult to be on an ID like this

+id  + genre                          + link

+1  +  first value of caption      + first value of href

+2  +  second value of caption  + first value of href

 

 

i need LOOP using foreach or while or something , i dont know exactely what to do

thanks for the help

Link to comment
Share on other sites

yeah its a function

 

 

    function show_caption($mode)        {        $this->get_links();	$bb="";            for ($i=1;$i<=count($this->caption);$i++)            {                if ($mode==0) echo $this->caption[$i]."\n<br>\n"; else echo $i."-->".$this->caption[$i]."\n<br>\n";				$bb .= $this->caption[$i];		}		return $bb;        } 

 

Link to comment
Share on other sites

You'd be better off returning an array that you can iterate over and add to the database, than a long string of data. If you want to make several entries into your db you'd first have to separate your string to iterate over that.

Link to comment
Share on other sites

You could just return $this->caption() in the method. The same with $this->href().

 

Assign them to a new array.

 

 

<?php$captions = $this->show_caption($caption); //(where the return is return $this->caption())$hrefs = $this->show_href($href); //(where the return is also the array of href's)for($i=0, $i<count($captions());$i++) {$sql = mysql_query("INSERT INTO `docs` (`artist` ,`genre` ,`link`)VALUES ('".addslashes($captions[$i])."', 'hiphop' ,'".$hrefs[$i])."')");}

 

 

the code for the method...

 

 

<?phpfunction show_caption($mode){$this->get_links();$bb="";for ($i=1;$i<=count($this->caption);$i++){	if ($mode==0) 		echo $this->caption[$i]."\n<br>\n"; 	else 		echo $i."-->".$this->caption[$i]."\n<br>\n";}return $this->caption;} 

 

 

You'll have to edit the show_href() method as well

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.