1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What is a good resource for PHP While statements?

Discussion in 'General PHP and MySQL Discussions' started by LPH, Jan 13, 2013.

  1. LPH

    LPH Well-Known Member

    Since I'm to the point of actually getting beyond connecting to the XF database ... now it's time to do something with it.

    Besides the php.net manual, is there a good resource on while statements?

    I'm stuck on the echo statement in the code below. The code only returns 1 result instead of 10. (HEY! At least I got this far).

    $db = XenForo_Application::getDb();
    if (!$db) {
        die('This script did not connect to the database' . mysql_error());
    $threads_qry = $db->query("
            SELECT *
            FROM `xf_thread`
            ORDER BY `last_post_date` DESC
            LIMIT 10
    while ($thread = $threads_qry->fetch()) {
                $latestthreads[$thread['thread_id']] = array(
            'id' => $thread['thread_id'],
            'title' => $thread['title'],
            'url' => XenForo_Link::buildPublicLink('canonical:threads', $thread),
            'replycount' => $thread['reply_count'],
            'dateline' => $thread['post_date']
    echo ("<a href='" . XenForo_Link::buildPublicLink('canonical:threads', $thread) .    "' >" . $thread['title'] . "</a><br />"); // Echo the title with a link. This returns ONE. Now what?  LOL
    // echo "These are the latest threads" .  . "<br />And now there should be ten.";
    Thanks for the resource links.
    Brandon Sheley likes this.
  2. borbole

    borbole Well-Known Member

    Try this:

    if (!
    $db) {
    'This script did not connect to the database' mysql_error());
    $query "SELECT * FROM `xf_thread`
            ORDER BY `last_post_date` DESC
            LIMIT 10
    $threads XenForo_Application::get('db')->fetchAll($query);
    foreach (
    $threads AS $thread)
    $latestthreads[$thread['thread_id']] = array(
    'id' => $thread['thread_id'],
    'title' => $thread['title'],
    'url' => XenForo_Link::buildPublicLink('canonical:threads'$thread),
    'replycount' => $thread['reply_count'],
    'dateline' => $thread['post_date']
    echo (
    "<a href='" XenForo_Link::buildPublicLink('canonical:threads'$thread) .    "' >" $thread['title'] . "</a><br />"); // Echo the title with a link. This returns ONE. Now what?  LOL
    // echo "These are the latest threads" .  . "<br />And now there should be ten.";

    About a tutorial site, there are quite a lot of them out there. Do a search in google and see which one will suit you best.

    P.s. For a basic explanation of stuff, have you checked tizag.com or the wc3schools?
    LPH likes this.
  3. LPH

    LPH Well-Known Member

    I'll try to figure out your approach. Thanks.

    Been googling all day :)

    Yes. I have been to both sites. Actual code examples are the most helpful for me. They allow me to figure out the approaches being used - because there are so many ways of doing things.

    Thank you again for the help!

    Update: Yes! Your approach worked. Now I have to figure out why :)
    borbole likes this.
  4. LPH

    LPH Well-Known Member

    And here it is ... all snuggled in at the bottom of the pages. If posts are not long enough then this floats to the left just below the primary container in WP. Otherwise .. this works.

    Thank you for your help.

    Screen Shot 2013-01-13 at 4.43.46 PM.png
    borbole and Brandon Sheley like this.
  5. borbole

    borbole Well-Known Member

    You can trim up the titles so they are all in the same character length. That should keep them in uniform and not stretched out. Add this code before the echo statement:

    //Trim thread titles
    if (strlen($thread['title']) > 20)
    $thread['title'] = substr($thread['title'], 020) . "...";
    And replace 20 with whatever character number that you want your thread titles to show.

Share This Page