Jump to content
MakeWebGames

DB Query Help


Miks

Recommended Posts

I was wondering if someone could kindly point me in the right direction as to where I am going wrong.

What I want to do is display the top 4 forum contributors above the forum, just to add some more stats and to fluff out the forum page a bit more.

At the moment all I want to do is display the results and then I will work them into the forum page.

Here is what I have:

 

<?php

include "globals.php";
$db->query("SELECT username FROM users ORDER BY posts DESC LIMIT 4");
$result = mysql_query($db);
while($row = mysql_fetch_array($result)) 
{
print "
$row
";
}

$h->endpage();
?>

 

I'm guessing its how I'm displaying the results but I am learning still

Link to comment
Share on other sites

You almost have it...there are a few things that have to be adjusted, but not too bad for just starting out.

I'll make the code change and note the differences for you for future reference.

 


include "globals.php";
$result = $db->query("SELECT username FROM users ORDER BY posts DESC LIMIT 4"); // moved $result to equal the query, as $db->query runs that query
// $result = mysql_query($db); Not needed
while($row = $db->fetch_row($result)) // using MCCodes fetch_row function, which can works as both fetch_array and fetch_row in mysql and mysqli
{
print $row['username']; // References the column itself (username) when printing. Also, no need for double quotes when you're solely printing a variable
}

$h->endpage();

 

Hopefully this helps. If you're still a little confused on exactly how this works, feel free to post questions on this thread about it. I'm sure if I cannot answer them for whatever reason, I'm sure others could help.

~G7470

Link to comment
Share on other sites

Ok I'm trying to style it and I cant seem to figure out where I am going wrong.

This is what I have done

 

<?php
include "globals.php";
$result = $db->query("SELECT username FROM users ORDER BY posts DESC LIMIT 4"); // moved $result to equal the query, as $db->query runs that query
// $result = mysql_query($db); Not needed
while($row = $db->fetch_row($result)) // using MCCodes fetch_row function, which can works as both fetch_array and fetch_row in mysql and mysqli
{
print "
<table class='mytable'>
<tr>
"; 
}
print "
<td>$row['username'] </td>
"; 
print "
</tr></table>
"; 
$h->endpage();
?>

 

So the goal is to put the results in a table on the same row i.e inbetween <td> </td>

Link to comment
Share on other sites

Look at where your while loop is located. Currently, this is your while loop:

 

while($row = $db->fetch_row($result)) // using MCCodes fetch_row function, which can works as both fetch_array and fetch_row in mysql and mysqli
{
print "
<table class='mytable'>
<tr>
";
}

 

Do you notice something wrong there? A couple things to note:

 

  • Where are your results from each row in this loop? They're not there, it's just formatting. Keep an eye out for when your loop starts and ends.
  • You should only be printing the <table> tag and the <tr> tag once. Right now, you're creating four tables and one row per table.

 

Those notes should at least help get you back on the right track. :)

~G7470

Link to comment
Share on other sites

Ok So I have put the table before the loop and now it works as intended. You were right, the way I had it before it was creating a table for each result.

Thanks for your time and help [MENTION=70485]G7470[/MENTION] you've helped me understand a lot more than what I understood this morning!

Link to comment
Share on other sites

I thought I would share the final result for anyone that would like to try it themselves

[ATTACH=CONFIG]1847[/ATTACH]

 

<?php

include "globals.php";

$result = $db->query("SELECT * FROM users ORDER BY posts DESC LIMIT 4"); 

print "
<table class='mytable'>
<tr>
"; 

{
while($row = $db->fetch_row($result)) 
print "
<td> <a href='viewuser.php?u={$row['userid']}'>{$row['username']}</a> <BR>
Posts:{$row['posts']}
</td>
"; 
}

print "
</tr></table>
"; 

$h->endpage();
?>

forum.thumb.png.75604a045d9ca7d75357edd4f2608aa1.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...