Printing results from query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amenhotep
    New Member
    • Sep 2006
    • 5

    Printing results from query

    Greetings.

    I have some problems which is not surprise as I'm a beginner in PHP.

    Here is my problem.

    I created database and table named base and news.
    In table news I created column: id, title, text, all_text, date... and category.

    I want to display last news by id and category. I used this:

    while($row = mysql_fetch_row ($result)) {
    $title[$i]=$row[1];
    $text[$i]=$row[2];
    $date[$i]=$row[4];
    $category[$i]=$row[6];
    ...........


    $i++;


    Well, this would print news as they were written.

    But, I want to print news from certain category, I used this:

    for($i=0;$i<siz eof($category); $i++) {
    if($category[$i]=="Art")

    $title2[$j]=$title[$i];
    $txt2[$j]=$text[$i];
    $date2[$j]=$date[$i];
    ......

    $j++;

    It worked well printing news from "ART".
    But after this, I want to print news from category SPORTS.

    How I'll do this? Please someone :(
  • Amenhotep
    New Member
    • Sep 2006
    • 5

    #2
    This doesnt work at all.

    It prints all news and not from ART category.

    :(

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      You have your data in a database but are trying to sort it yourself.

      This is the proverbial having a dog and barking yourself.

      In your SELECT statement use a WHERE clause to filter on the category that you wish to use, forget about trying to do this kind of sort yourself.

      Comment

      • Amenhotep
        New Member
        • Sep 2006
        • 5

        #4
        Ok, but I want to display 5 news from category, than 5 new from other category, and so on.

        Comment

        • Amenhotep
          New Member
          • Sep 2006
          • 5

          #5
          I found the solution.

          Comment

          • rkagrawal
            New Member
            • Aug 2006
            • 13

            #6
            Can you please post the solution here so that everyone can be benefitted out of it ?

            I have a solution in mind but i am not very sure if it will work.
            I want to see your solution first.

            Rahul Agrawal
            Developer
            Web2003 Corporation
            www.web2003corp .com

            Comment

            • Amenhotep
              New Member
              • Sep 2006
              • 5

              #7
              Originally posted by rkagrawal
              Can you please post the solution here so that everyone can be benefitted out of it ?

              I have a solution in mind but i am not very sure if it will work.
              I want to see your solution first.

              Rahul Agrawal
              Developer
              Web2003 Corporation
              www.web2003corp .com
              Ok, the solution I came to is to have separate queries for each category i want to display news from.
              It can be made in more efficient way, but hey! I'm not fimiliar with PHP. It could be done trough WHILE, IF, ELSE and FOR. but...

              So,
              I made this:

              $query1 = "SELECT * FROM news WHERE category='arts' ORDER BY id DESC LIMIT 5";
              $query2 = "SELECT * FROM news WHERE category='polit ics' ORDER BY id DESC LIMIT 5";

              than defined variables to carry values from selected rows, than print them.
              variable $title for first category will be equal to =row[1]
              but $title for other category will be - $title1[$i]=row[1]

              maybe modest, for some stupid, but it works for me!

              Comment

              Working...