Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:21:50+00:00 2026-06-08T04:21:50+00:00

I have a news table and I want to echo out a different statement

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:21:52+00:00Added an answer on June 8, 2026 at 4:21 am

    To order the results by date you could extend your query like this

    SELECT * FROM news ORDER BY `submission_date` DESC //or "ASC" for opposite order
    

    by using your column name. To handle different results

    while ($row = mysql_fetch_assoc($result)) { //for each returned row
      switch($row['type']){ //your column name for field
        case 'release_date':
          echo $row['name']." will be released on";
          break;
        case 'new_video':
          echo $row['name']." has a new video";
          break;
          //other cases
        default: //if there should be a process for "anything else"-cases
          //code
          break;
       }
    }
    

    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:

    $html = ''; //set variable
    while ($row = mysql_fetch_assoc($result)) { //for each returned row
      switch($row['type']){ //your column name for field
        case 'release_date':
          $html .= "<p>".$row['name']." will be released on</p>";
          break;
        case 'new_video':
          $html .= "<p>".$row['name']." has a new video</p>";
          break;
          //other cases
        default: //if there should be a process for "anything else"-cases
          //code
          break;
       }
    }
    echo $html; 
    /*$html is now filled with some paragraphs.
    You should echo it inside a complete html-structure 
    with doctype and inside the body-tag.
    The position of the echo inside you script depends on the rest of your code. */
    

    Because you’re inserting userdefined content make sure that the inputs are

    1. escaped
    2. trimmed or the switch-case may not work

    And please notice eggyal’s comment, I’ve just taken your code. Do not use mysql_query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jquery/ajax load new content when available i have a table named news
I have a table Resource with a field Type . Type is a lookup
How do I do this? I have the table news and I want the
I have a Content Type News with custom field NewsDate (DateTimeField) and Active (BooleanField)
I have never tried this but want to echo data from several different tables
I have a database table containing the news on my site. It's divided into
I have tables named news and tags_news. Given a news id I have to
I have two classes: News : /** @Entity @Table(name=news) */ class News { /**
I have there is a table showing the last 10 news. But when I
I have a table with the following rows: id. user_id, type - link 1.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.