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

by 3 replies
4
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
#programming #array #elements #mysql #php #pulling #stuck
  • 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;
    • [ 1 ] Thanks
    • [1] reply
  • 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.

Next Topics on Trending Feed