Im stuck with pulling array elements from mysql DB with PHP please help

3 replies
I want to randomly display bits of text from my DB in the title tags and other places on my site, but I cant figure out how.

First I used

$keyword_one = $result['keyword_one'];
$keyword_two = $result['keyword_two'];
$keyword_three = $result['keyword_three'];
$keyword_four = $result['keyword_four'];
$keyword_five = $result['keyword_five'];
$keyword_multi = $result['keyword_multi'];

$input = array($keyword_one, $keyword_two, $keyword_three, $keyword_four, $keyword_five);
$keyword = array_rand($input);

// echo $input[$keyword];
?>

and i worked fine, but I didnt want a brand new column in the DB for every snippet so I tried this

$keyword_one = $result['keyword_one'];
$keyword_two = $result['keyword_two'];
$keyword_three = $result['keyword_three'];
$keyword_four = $result['keyword_four'];
$keyword_five = $result['keyword_five'];
$keyword_multi = $result['keyword_multi'];

$input = array($keyword_multi);
$keyword = array_rand($input);

// echo $input[$keyword];
?>

and stored this in the DB

'best rated e cigarette 2017', 'most popular e cig 2017', 'best priced e cigarette', 'number one voted e cigarette worldwide', 'the longest established e cig supplier','number one e cig 2017', 'get healthy with e cigs', 'forget the craving smoke anywhere', 'we all love to smoke and with this e cig you can do it anywhere', 'the new super e cigarette for everyone'

can anyone tell me where im going wrong or do I need to use a different column for every keyword??

I want to do this with random content sections to but a column per one would be a nightmare

can anyone help please
#array #elements #mysql #php #pulling #stuck
  • Profile picture of the author timokeefe
    You can actually do it with one column all through MySQL. Suppose everything is in a column called 'text' in a table called 'content', you can query out random values like so:

    Code:
    SELECT
      c.text
    FROM
      content c
    ORDER BY rand() LIMIT 5;
    Signature

    I'm part of the Warrior Forum team, hit me up with any suggestions that could help improve the forum!

    {{ DiscussionBoard.errors[11082182].message }}
  • Profile picture of the author timokeefe
    No problems Michael, I don't think that "ORDER BY rand()" trick is super well known, even though it's really useful for analysing certain data sets.
    Signature

    I'm part of the Warrior Forum team, hit me up with any suggestions that could help improve the forum!

    {{ DiscussionBoard.errors[11082548].message }}

Trending Topics