Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to run an insert query in a loop until all the elements from an array pass.

Ex:

$signs = array("aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio", "sagittarius", "capricorn", "aquarius", "pisces");


$config = array('sign' => 'aries',
                'type' => 'daily',
                'date' => date('Y-m-d'),);


$content = $horoscope->get_daily_horoscope($config);
$update = $db->prepare("INSERT INTO `horoscope` (`zodiacal_sign`, `last_updated`, `content`) values (%s,%s,%s)", $config['sign'], $config['date'], $content);
$db->query($update);

I don't know how to run this query until all of $signs replaces $config['sign'] and the query runs all the 12 times in a loop.

Can somebody help me?

share|improve this question
 
The question is not so clear maybe you could try array_pop in a for loop if i have understood some of it. –  woofmeow Aug 7 '13 at 1:18
add comment

1 Answer

up vote 3 down vote accepted

You can do it like this..

$signs = array("aries", "taurus", "gemini", "cancer", "leo", "virgo", "libra", "scorpio",     "sagittarius", "capricorn", "aquarius", "pisces");

foreach($signs as $s){


    $config = array('sign' => $s,
            'type' => 'daily',
            'date' => date('Y-m-d'),);


    $content = $horoscope->get_daily_horoscope($config);
    $update = $db->prepare("INSERT INTO `horoscope` (`zodiacal_sign`, `last_updated`, `content`) values (%s,%s,%s)", $config['sign'], $config['date'], $content);
    $db->query($update);
}
share|improve this answer
 
Thank you, that worked. well... almost. max_execution_time is set to 30 and the script tages longer. do you think there is a way to split $signs in two and run the two queries one by one (first 6 signs and next the last 6 of them)? –  m3tsys Aug 7 '13 at 1:59
 
yes you can. just split the $signs to two and do two foreach() statement. –  shark Aug 7 '13 at 2:32
 
i've tried that but that is the exactly the same thing... –  m3tsys Aug 7 '13 at 21:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.