0

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?

1
  • The question is not so clear maybe you could try array_pop in a for loop if i have understood some of it. Commented Aug 7, 2013 at 1:18

1 Answer 1

3

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

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)?
yes you can. just split the $signs to two and do two foreach() statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.