What is a good resource for PHP While statements?

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).

Code:
$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.
 
Try this:

PHP:
$db = XenForo_Application::get('db');
 
if (!$db) {
 
    die('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?
 
  • Like
Reactions: LPH
Try this:

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

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?

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 :)
 
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.webp
 
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.


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:

PHP:
//Trim thread titles
if (strlen($thread['title']) > 20)
{
    $thread['title'] = substr($thread['title'], 0, 20) . "...";
}

And replace 20 with whatever character number that you want your thread titles to show.
 
Back
Top Bottom