Very confusing mySQL array

This is a discussion on "Very confusing mySQL array" within the PHP Forum section. This forum, and the thread "Very confusing mySQL array are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Oct 7th, 2007, 02:19
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Very confusing mySQL array

i'm pulling everything from my blog entries table:
Select * from blog_entries;

this has 6 columns:
id
entry
title
timestamp
rating
cat (category)

How would I go about doing this:
<div class="blogentry">
<strong>Title from mysql</strong> - <span class="date">DATE</span>
<span class="category">Category</span>
<p>a bit of the entry, cut off after a while</p>
<div class="rating">
rating
</div>
</div>


for each row?
it confuses me, i don't know how many arrays are in each array and stuff like that.

Help appreciated, sorry if I didn't explain this properly.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Oct 7th, 2007, 03:23
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Very confusing mySQL array

Hi Alex,

The preferred way is to loop through the results of the query with a while loop.

eg.

PHP: Select all



$results 
mysql_query ($query) or die (mysql_error() . '<br />Ouch!!');

// check there were *SOME* results

if (mysql_num_rows($results))
{
    
// loop through the array
    
    
while ($row mysql_fetch_assoc($results)) // Will fetch an associative array with the column names as the keys
        
    
echo '<div class="blogentry">
        <strong>'
$row['title'], '</strong> - <span class="date">'strftime('%d %b %Y'$row['date']), '</span>
        <span class="category">'
$row['cat'], '</span>
        <p>'
substr($row['entry'], 0100), '.....</p>'// change 100 to be how many characters you want
        
'<div class="rating">',
            
$row['rating'],
        
'</div>
        </div>'
;
} else
    echo 
'<strong>No Results returned! Oh the agony!</strong>'
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Oct 7th, 2007, 16:12
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Very confusing mySQL array

unexpected string with the while loop unfortunately. I don't know why though :/
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Oct 7th, 2007, 16:15
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Very confusing mySQL array

Woops, it works great thanks notepad messed up some of the asciis for something. Thanks Rakuli yet again!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Oct 7th, 2007, 16:21
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Very confusing mySQL array

Could you explain the while statement please? I really don't get it.. and why doesn't the while loop have curly braces? This really confuses me. thanks
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Oct 8th, 2007, 08:25
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Very confusing mySQL array

No probs.

The mysql_fetch_array/row/array functions keep track of the last array index that was retrieved. This means that each time you call the function you get the next index of the array until there are none remaining when it will return false.

Using this in a while loop means that when the statement evaluates to false (when no results remain) the conditional fails and the loop breaks.

The reason why there are no curly braces is because there is only one statement in the loop. Curly braces aren't required for conditionals and loops unless you need to execute multiple statements.

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Oct 8th, 2007, 12:30
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Very confusing mySQL array

Oh thanks
How can I just loop through a certain number then?
or have 10 entries per page?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Oct 8th, 2007, 12:38
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Very confusing mySQL array

To loop through a certain number, the best thing would be to use a for loop

PHP: Select all



// max results to output
$max 10;

for (
$i=0$i $max$i++)
    echo 
'random txt is fun to right'// So on.... 
Pagination takes a somewhat more complex approach.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Oct 8th, 2007, 17:19
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Very confusing mySQL array

Thanks Rakuli, I shall try to explain more though. (Sorry to ask so much).
how would I do it with the following:
archives.php?s=1&e=10
archives.php?s=11&e=20

so, the first would show blogs 1 - 10 and the second would show 11 - 20 so on so forth?
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Oct 8th, 2007, 17:27
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Very confusing mySQL array

Hi alex,

Have a look at the code I wrote in This thread about paginationp

It has an example of what you want to do.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Oct 8th, 2007, 17:29
alexgeek's Avatar
Moderator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,812
Blog Entries: 9
Thanks: 2
Thanked 2 Times in 2 Posts
Re: Very confusing mySQL array

Thanks Rakuli.
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
checkbox[] array into mysql... OUT? ppgpilot PHP Forum 1 Mar 13th, 2008 20:37
float issue (confusing) danny322 Web Page Design 6 Jan 10th, 2008 08:28
Confusing Clock adrix Flash & Multimedia Forum 5 Aug 31st, 2006 14:26
Uploading form-generated array to MySQL masonbarge PHP Forum 1 Jul 15th, 2006 17:46
array unable to check another array so as to be displayed Ozeona Flash & Multimedia Forum 1 Aug 5th, 2005 10:26


All times are GMT. The time now is 13:15.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42