I have a news table and I want to echo out a different statement depending on what type of news it is. The type is stored in the field named “type” in the table.
Here’s the basic code I’m using to return the data to an array
$query = mysql_query("SELECT * FROM news");
while ($row = mysql_fetch_assoc($result)) {
}
So at the moment I’m returning all the information from the table ‘news’ but I want to echo out different statements depending on the type within the row.
The purpose of the table is to display user submitted news with different pre-defined text based on the type field.
To give an example of what I mean: if the row contains the type “release_date” echo out ”name’ will be released on”. If the row contains the type of new_video echo “‘name’ has a new video”. It also then needs to be ordered by the submission date which is also contained in the table.
I don’t want to do this with seperate queries because I need to arrange all of the news by submission date.
Would the best way of doing this be with an if statement? I’m a bit lost for ideas at the moment, any advice will be really appreciated. Hope this makes sense
To order the results by date you could extend your query like this
by using your column name. To handle different results
edit:
"echo" at this point won’t make you happy; I guess you want to output the results in html; then you have to define a variable before the loop starts and fill it inside the loop. Like this:
Because you’re inserting userdefined content make sure that the inputs are
And please notice eggyal’s comment, I’ve just taken your code. Do not use mysql_query.