Question

If I have a MySQL query that looks like this:

SELECT Content FROM AP_Data WHERE Title='Featured' or Title='Carousel'

Is it possible to use PHP to then turn the variables into something like

echo $Row['Featured']['Content'];
echo $Row['Carousel']['Content'];

I hope that by doing this, I can save time without bombarding the server with multiple queries. This is only a snippet of the queries I require.

Was it helpful?

Solution

First lets update your query to include the Title field.

SELECT Content, Title FROM AP_Data WHERE Title='Featured' or Title='Carousel'

When looping through results you can build the array to suit your needs

<?php
while($row = your_query_result){

  //If you have one Content per Title   
  $array[$row['Title']] = $row['Content'];

  //If you have multiple Contents per Title
  $array[$row['Title']][] = $row['Content'];

}
?>

OTHER TIPS

Featured and Carousel are not column names , they just values in columns.

you can do this instead.

    SELECT Content,Title FROM AP_Data WHERE Title='Featured' or Title='Carousel'

and the code

if  ($Row['Title'] == 'Featured') 
    {echo $Row['Content']; // content of featured
    } 
else{
    echo $Row['Content']; //content of carousel
    }

No, it does not work that way. But if you add Title to your query, like so:

SELECT Title,Content FROM AP_Data WHERE Title='Featured' or Title='Carousel'

You can then build an array like that. You can also do an ORDER BY Title so that all of the Carousel results would be first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top