SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This code just will not work, why does it keep displaying:

    "Site News" and "Web News" ??

    It is suppoesed to create two lists:

    Site News:
    -blah
    -blah
    -blah
    -blah

    Web news:
    -blah
    -blah
    -blah
    -blah

    But it doesn't,it does this:
    Code:
    Site News: 
    -blah
    
    Site News: 
    -blah
    
    Web News: 
    -blah
    
    Site News: 
    -blah
    Here is the code:
    Code:
    <html>
    <head>
    <title>News</title>
    <link rel="stylesheet" href="http://www.pcreview.co.uk/style.css" type="text/css">
    </head>
    
    <body bgcolor="#FFFFFF">
    <p><font size="7" color="#0066CC">News</font></p>
    <P ALIGN=CENTER><A HREF="addnews.php">Create a New News Article</A></P>
    <UL>
    <?php
    
    include("articles.inc");
    mysql_select_db('ianc_articles');
    
    $news = mysql_query("SELECT ID, Title, Author, Status FROM news");
    if (!$news) {
      echo("<P>Error retrieving authors from database!<BR>".
           "Error: " . mysql_error());
      exit();
    }
    
    while ($select = mysql_fetch_array($news)) {
    $aid = $select["ID"];
    $title = $select["Title"];
    $name = $select["Author"];
    $status = $select["Status"];
    
    $a = 1;
    $b = 1;
    $c = 1;
    $d = 1;
    
    
    if ($status == 1) {
    
    if ($a == 1) {
    print "<font size=4>Site News</font><br>";
    
     $a++;
    }
    
    if ($b < 11) { 
    
      echo("<LI>$title<br><font size='-1' color='blue'>By $name</font> " .
           "[<A HREF='editnews.php?id=$aid'>Edit</A>|".
           "<A HREF='deletenews.php?id=$aid'>Delete</A>]<br><br>");
    
     
    $b++; } 
    
    } 
    
    if ($status == 2) {
    
    if ($c == 1) {
    print "<font size=4>Web News</font><br>";
    
    $c++; } 
    
    if($d < 11) {
    
    echo("<LI>$title<br><font size='-1' color='blue'>By $name</font> " .
           "[<A HREF='editnews.php?id=$aid'>Edit</A>|".
           "<A HREF='deletenews.php?id=$aid'>Delete</A>]<br><br>");
    
    $d++; } 
    }
    }
    
    ?>
    </UL>
    <P ALIGN=CENTER><A HREF="index.php">Return to Front Page</A></P>
    
    </body>
    </html>
    Thanx, Peter

  2. #2
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is when code indentation helps.
    Check your while loop, where should it ended. Since I believe the Site News / Web News stay within the loop and keep displaying.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    They should stay within the loop otherwise the results are not displayed at all.

    It just seams that the if statements aren't working at all.

    Peter

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because evrytime it loops you check for status == 1 or == 2 and then display the header and then the item so each time it is gonna display the ehader and the item. Iwould put them into an array and then loop through it twice once for the web news once for the site news, I would take this approach

    Code:
    <html>
    <head>
    <title>News</title>
    <link rel="stylesheet" href="http://www.pcreview.co.uk/style.css" type="text/css">
    </head>
    
    <body bgcolor="#FFFFFF">
    <p><font size="7" color="#0066CC">News</font></p>
    <P ALIGN=CENTER><A HREF="addnews.php">Create a New News Article</A></P>
    <UL>
    <?php
    
    include("articles.inc");
    mysql_select_db('ianc_articles');
    
    $news = mysql_query("SELECT ID, Title, Author, Status FROM news");
    if (!$news) {
      echo("<P>Error retrieving authors from database!<BR>".
           "Error: " . mysql_error());
      exit();
    }
    
    while ($select = mysql_fetch_array($news)) {
    	$items[] = array("aid" => $select["ID"],
    					 "title" => $select["Title"],
    					 "name" => $select["Author"],
    					 "status" => $select["Status"]
    					 );
    
    	}
    
    
    print "<font size=4>Site News</font><br>";
    foreach($items as $key => $val) {
    	printf('<LI>%s<br><font size="-1" color="blue">By %s</font>[<A HREF="editnews.php?id=%s">Edit</A>|<A HREF="deletenews.php?id=%s">Delete</A>]<br><br>', $items[$key]["title"], $items[$key]["name"], $items[$key]["aid"], $items[$key]["aid"]);
    	}
    reset($items);
    print "<font size=4>Web News</font><br>";
    foreach($items as $key => $val) {
    	printf('<LI>%s<br><font size="-1" color="blue">By %s</font>[<A HREF="editnews.php?id=%s">Edit</A>|<A HREF="deletenews.php?id=%s">Delete</A>]<br><br>', $items[$key]["title"], $items[$key]["name"], $items[$key]["aid"], $items[$key]["aid"]);
    	}
    
    ?>
    </UL>
    <P ALIGN=CENTER><A HREF="index.php">Return to Front Page</A></P>
    
    </body>
    </html>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Freddy the code works perfectly.However I would like to know if there is an easier way of limiting each news type to ten than

    Code:
    $i = 1;
    if ($i < 11 ) {
    
    print stuff
    }
    Thanx

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    while ($select = mysql_fetch_array($news)) {
    	if ($select["status"] == 1) {
    		$site_items[] = array("aid" => $select["ID"],
    						 "title" => $select["Title"],
    						 "name" => $select["Author"],
    						 "status" => $select["Status"]
    						 );
    		}
    	if ($select["status"] == 2) {
    		$web_items[] = array("aid" => $select["ID"],
    						 "title" => $select["Title"],
    						 "name" => $select["Author"],
    						 "status" => $select["Status"]
    						 );
    		}					 
    	}
    
    
    print "<font size=4>Site News</font><br>";
    for($i=0;$i<10;$i++) {
    	printf('<LI>%s<br><font size="-1" color="blue">By %s</font>[<A HREF="editnews.php?id=%s">Edit</A>|<A HREF="deletenews.php?id=%s">Delete</A>]<br><br>', $site_items[$i]["title"], $site_items[$i]["name"], $site_items[$i]["aid"], $site_items[$i]["aid"]);
    	}
    print "<font size=4>Web News</font><br>";
    for($i=0;$i<10;$i++) {
    	printf('<LI>%s<br><font size="-1" color="blue">By %s</font>[<A HREF="editnews.php?id=%s">Edit</A>|<A HREF="deletenews.php?id=%s">Delete</A>]<br><br>', $web_items[$i]["title"], $web_items[$i]["name"], $web_items[$i]["aid"], $web_items[$i]["aid"]);
    	}
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    That code producs the two sets of ten news items with bullet points. However the news it missing.

    There are just bullet points and the Edit / Delete links with no ID

    I think you missed a for each function, but i tried putting in but it didn't work

    Peter

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure? You did change the upper part too, right?> So you create two arrays not just one. And you changed the variables at the end of each printf() statement to look like mine?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  9. #9
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I copied the whole thing over,

    Code:
    <html>
    <head>
    <title>News</title>
    <link rel="stylesheet" href="http://www.pcreview.co.uk/style.css" type="text/css">
    </head>
    
    <body bgcolor="#FFFFFF">
    <p><font size="7" color="#0066CC">News</font></p>
    <P ALIGN=CENTER><A HREF="addnews.php">Create a New News Article</A></P>
    <UL>
    <?php
    
    include("articles.inc");
    mysql_select_db('ianc_articles');
    
    $news = mysql_query("SELECT ID, Title, Author, Status FROM news");
    if (!$news) {
      echo("<P>Error retrieving authors from database!<BR>".
           "Error: " . mysql_error());
      exit();
    }
    
    while ($select = mysql_fetch_array($news)) {
    	if ($select["status"] == 1) {
    		$site_items[] = array("aid" => $select["ID"],
    						 "title" => $select["Title"],
    						 "name" => $select["Author"],
    						 "status" => $select["Status"]
    						 );
    		}
    	if ($select["status"] == 2) {
    		$web_items[] = array("aid" => $select["ID"],
    						 "title" => $select["Title"],
    						 "name" => $select["Author"],
    						 "status" => $select["Status"]
    						 );
    		}					 
    	}
    
    ?>
    <table width="80%" border="0">
    <tr><td>
    <?php
    
    print "<font size=4>Site News</font><br>";
    for($i=0;$i<10;$i++) {
    
    
    	printf('<LI>%s<br><font size="-1" color="blue">By %s</font>[<A HREF="editnews.php?id=%s">Edit</A>|<A HREF="deletenews.php?id=%s">Delete</A>]<br><br>', $site_items[$i]["title"], $site_items[$i]["name"], $site_items[$i]["aid"], $site_items[$i]["aid"]);
    	}
    
    
    ?>
    </td><td>
    <?php
    
    print "<font size=4>Web News</font><br>";
    for($i=0;$i<10;$i++) {
    
    	printf('<LI>%s<br><font size="-1" color="blue">By %s</font>[<A HREF="editnews.php?id=%s">Edit</A>|<A HREF="deletenews.php?id=%s">Delete</A>]<br><br>', $web_items[$i]["title"], $web_items[$i]["name"], $web_items[$i]["aid"], $web_items[$i]["aid"]);
    
    }
    ?>
    </td></tr></table>
    
    
    
    
    </UL>
    <P ALIGN=CENTER><A HREF="index.php">Return to Front Page</A></P>
    
    </body>
    </html>

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this to see if the arrays are getting populated:

    Code:
    while ($select = mysql_fetch_array($news)) {
    	if ($select["status"] == 1) {
    		$site_items[] = array("aid" => $select["ID"],
    						 "title" => $select["Title"],
    						 "name" => $select["Author"],
    						 "status" => $select["Status"]
    						 );
    		}
    	if ($select["status"] == 2) {
    		$web_items[] = array("aid" => $select["ID"],
    						 "title" => $select["Title"],
    						 "name" => $select["Author"],
    						 "status" => $select["Status"]
    						 );
    		}					 
    	}
    
    
    print count($site_items)."<br>";
    print count($web_items);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  11. #11
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    outputs 0 0

    However never mind, I found the problem.

    $select["status"] should have been $select["Status"]

    CaPiTALs Suck...

    Thanx allot Freddy

    -Peter

  12. #12
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yep, capitalisation sucks. That's why you need to develop some coding conventions. Here are mine:

    Class name - capitalise the first letter:
    eg; Animal, Dog, Cat

    I do the same with database table names (because in OO-Relational mapping you are usually going to have a table for each class).

    Variable names and function names - lower case, except capitalise the first letter of the second, third, etc word for readability:
    eg; $firstName, $secondName, $nextOfKin
    eg; function saveNewCustomer()

    Same capitalisation for database fields (columns) because these usually map to variables or Class data members.

    File names. I always use all lower case and seperate words with an underscore:
    eg; new_customer_sign_up.html
    Last edited by freakysid; Mar 26, 2001 at 21:46.

  13. #13
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's why I love Java, strictly enforce you to use capital letters properly. It's tatooed in my head
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •