Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP programming problem!!

  • 14-01-2006 11:22pm
    #1
    Closed Accounts Posts: 1


    Hi!

    I am having many problem with a simple PHP program!

    I want to read the most recent (100 most recent) database posts and display them. My code work but it is dirty! Does anyone know an easier way??!

    [PHP]
    echo "<html><body>";

    // connect to database

    $my_db = mysql_connect($dbhost, $dbuser, $dbpass) or die("Could not connect to DB. Error: ".mysql_error());
    mysql_select_db($db) or die("Could not select DB. Error: ".mysql_error());

    // find out how many rows in the table

    $my_db_rows = "select count(keywords) from keywords";
    $my_db_numrows = mysql_query($my_db_rows) or die("Could not get number of rows. Error: ".mysql_error());
    $my_dbs_rows = mysql_fetch_row($my_db_numrows);

    // the last row

    $my_dbs_rows[0]--;

    // the 100th row before the last row

    $my_db_last_100 = ($my_dbs_rows[0] - 99);

    // our query

    $my_db_query = "select keywords from keywords limit ".$my_db_last_100.",".$my_dbs_rows[0]."";
    $the_rows = mysql_query($my_db_query) or die("Could not retrieve most recent 100 rows. Error: ".mysql_error());

    // print our results in HTML

    $counter = 0;
    $newarray = array();
    while($each_keyword = mysql_fetch_array($the_rows))
    {
    $newarray[$counter] = $each_keyword[0];
    $counter++;
    }

    for($i=99;$i>-1;$i--)
    echo "$newarray[$i]<br>";

    mysql_free_result($my_db_numrows);
    mysql_free_result($the_rows);

    mysql_close($my_db);


    echo "</body></html>";
    [/PHP]

    As you can see I am selecting each database item and then putting it into another array, and then reading the new array backwards!

    Is there easier way?


Comments

  • Registered Users, Registered Users 2 Posts: 885 ✭✭✭clearz


    Look at the SQL
    ORDER BY <field> DESC
    ORDER BY <field> ASC

    http://www.w3schools.com/sql/sql_orderby.asp

    Then the database will handle the order that you reciece the data in.
    That way you can do away with that messy array code and output the text from within the while loop
    [COLOR=#000000][COLOR=#007700]while([/COLOR][COLOR=#0000bb]$each_keyword [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]mysql_fetch_array[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$the_rows[/COLOR][COLOR=#007700])) 
    { 
        echo  [/COLOR][COLOR=#007700] [/COLOR][COLOR=#0000bb]$each_keyword[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000bb]0[/COLOR][COLOR=#007700]];[/COLOR][COLOR=#007700]
    } [/COLOR][/COLOR]
    


  • Registered Users, Registered Users 2 Posts: 689 ✭✭✭JoeB-


    Hi
    Could you not use SQL like...

    $query = "SELECT keywords FROM keywords ORDER BY keywordID desc LIMIT 100";

    this should give the most recent 100 entries as ordered by the 'keywordID' column, or whatever colume you use for ordering... if there aren't 100 entries, no problem, all are returned...

    You could use table HTML in the loop to make the output more pretty...
    echo "<td> .$yourValue </td><br>"


    ... the following code displays info from multiple columns and rows in a table format... all you have to do is supply the query...

    $result = mysql_query($query);
    $numFields = mysql_num_fields($result);

    echo "<table>";
    while ($row = mysql_fetch_row($result)) {
    echo "<tr>"; //Start row on table
    $rowNum += 1;
    echo "<td>" . $rowNum . "</td>";

    for ($i=0; $i<=$numFields-1; $i++) {
    echo "<td>" . $row[$i] . "</td>"; //Output all fields in row...
    }

    echo "</tr>"; //This ends this row on table...
    }
    echo '</table>'; // End Table


    I've previewed this and it's hard to follow, all the tabs are removed, it'll make more sense if tabbed correctly to show the loops...

    Cheers
    Joe


  • Registered Users, Registered Users 2 Posts: 885 ✭✭✭clearz


    Hi
    Could you not use SQL like...

    $query = "SELECT keywords FROM keywords ORDER BY keywordID desc LIMIT 100";

    this should give the most recent 100 entries as ordered by the 'keywordID' column, or whatever colume you use for ordering... if there aren't 100 entries, no problem, all are returned...

    You could use table HTML in the loop to make the output more pretty...
    echo "<td> .$yourValue </td><br>"


    ... the following code displays info from multiple columns and rows in a table format... all you have to do is supply the query...

    $result = mysql_query($query);
    $numFields = mysql_num_fields($result);

    echo "<table>";
    while ($row = mysql_fetch_row($result)) {
    echo "<tr>"; //Start row on table
    $rowNum += 1;
    echo "<td>" . $rowNum . "</td>";

    for ($i=0; $i<=$numFields-1; $i++) {
    echo "<td>" . $row[$i] . "</td>"; //Output all fields in row...
    }

    echo "</tr>"; //This ends this row on table...
    }
    echo '</table>'; // End Table


    I've previewed this and it's hard to follow, all the tabs are removed, it'll make more sense if tabbed correctly to show the loops...

    Cheers
    Joe


    Diddnt I just say that.


Advertisement