General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 7th, 2003, 08:34 PM
guitarnoise guitarnoise is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 32 guitarnoise User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
splitting results into columns

Hey everybody,
I really enjoy making top ten lists on my site. I was wondering what the trick is to get the results 1-5 in one column and 6-10 in another? Just like on the DevArtivles home page. It looks so much better that way, but this probably simple trick eludes me. Can anyone give me an example or point me in the right direction? Thanks.
__________________
Without me my guitar is useless
--
http://www.guitarnoise.com
http://www.musiccareers.net

Reply With Quote
  #2  
Old June 8th, 2003, 05:09 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
ColumnSplit

I have the example you are looking for:
http://www.laidbak.net/columnsplit/

You can download the source here:
http://www.laidbak.net/columnsplit/columnsplit.tgz


You can even change the number of columns and watch
the table change in shape. The cell items will figure out
where to go.
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old June 8th, 2003, 12:41 PM
guitarnoise guitarnoise is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 32 guitarnoise User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Hi Wil,

Thanks for the example and code. It's pretty cool and helped me accomplish about half of what I'm trying to do. I am a bit of a beginner and am not too clear on the arrays.

In your code you have preset items for all the 50 States.

$items = array('ALABAMA',' etc...

How would I go about getting the items to be populated with the names of my ten most popular articles? I tried the way I would when using a single column table but that's no good here.

Reply With Quote
  #4  
Old June 8th, 2003, 06:47 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Its kinda hard to figure out what you want to do when you posted no code.

However, I'll attempt to help you the best I can by guessing.

I assume you have articles stored in a mysql database.

You are most likely running a select query, and you want to put the results into an array such as the one I show above.

You can loop through your query results like the following:
Code:

$article_list = array();
while ($article = mysql_fetch_object($result))
{
  array_push($article_list, $article->article_name);
}

This is the same thing as populating your array manually as I did at the top.

Reply With Quote
  #5  
Old June 8th, 2003, 08:41 PM
guitarnoise guitarnoise is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 32 guitarnoise User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Thanks again. You're right that I have articles in mysql and want to put them in an array to work with your column sort.

My code looks like this:

Code:
  $result    = mysql_query("SELECT ID, ArticleTitle, Count ".
                           "FROM CountArticle WHERE ID > 0 ".  
                           "ORDER BY Count DESC LIMIT 10 ");
   if (!$result) {
    echo("Error: " . mysql_error());
    exit();
   }
  while ($article = mysql_fetch_array($result)) {
    $id       = $article["ID"];
    $articletitle = $article["ArticleTitle"];
    echo("$articletitle");
  }


I'm still not sure where your array loop should go. I'd like to have the ten results fill out the $items array in your column split example.

Reply With Quote
  #6  
Old June 8th, 2003, 10:00 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
My code needs to be below yours.
You just need to put your info into the array.

You will need to change your code to something like the following:

PHP Code:
 $result    mysql_query("SELECT ID, ArticleTitle, Count ".
                           
"FROM CountArticle WHERE ID > 0 ".  
                           
"ORDER BY Count DESC LIMIT 10 ");
   if (!
$result) {
    echo(
"Error: " mysql_error());
    exit();
   }

$items = array();

  while (
$article mysql_fetch_array($result)) {
    
$items[] = array("id"=> $article["ID"], "articletitle"=>$article["ArticleTitle"]);
  } 

Reply With Quote
  #7  
Old June 9th, 2003, 07:16 PM
guitarnoise guitarnoise is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 32 guitarnoise User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Well, I tried the code you gave above and some variations of it. I must still be missing something though. I can make a table with 10 cells, which is what I need. But it is inserting identical values into each cell. Each cell appears with "Array" in it. What am I doing wrong? How do I get the actual article title to show up?

Reply With Quote
  #8  
Old June 9th, 2003, 11:13 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
your $items array is not just a simple variable... it is an array.
An array is more than a variable because it is somewhat of a storage object.

Every time you add an item to this storage object you create what we call a new index. Usually indexes are incremented by one each time.

Your first index is usually 0, the next is 1, then 2, etc.

When you print a simple variable you do something like this:

print $item;

Unfortunately $item in this case is not a simple variable. You have to specify which part of the storage object you want to print out such as:

print $item[0];

That would print the first value stored in that array.

print $item[9];

That prints the tenth value.

So, when we call the function array_push($item, "somevalue");

We are essentially incrementing the number of items inside of the array called $item, and we are making its value "somevalue".

If you call this function 10 times, you will end up with an array with 10 values inside.


If you need more help with this take your time and read:
http://us3.php.net/manual/en/language.types.array.php


Hope this helps,


--
Wil Moore III

Reply With Quote
  #9  
Old June 10th, 2003, 10:12 AM
guitarnoise guitarnoise is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 32 guitarnoise User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Well, going back to my original post I actually described the 2 column top ten list as a simple trick. It's turning into more than that. I know to design with php I am going to have to come to grips with arrays but I have to admit I don't understand them very well yet.

I'll keep going over the info at the link you sent. With time it will come. In the meantime, are there any others ways to code a simple 2 column top ten list?

Thanks for the great help so far.


Reply With Quote
  #10  
Old June 10th, 2003, 06:03 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
I thought I gave it to you as simple as it can get.
Maybe someone else has some input here as to how to make this easier on you.

But you know, this post has gone on for about 2 or 3 days, when it could have taken 2 or 3 hours to understand php arrays.

Reply With Quote
  #11  
Old June 10th, 2003, 07:00 PM
guitarnoise guitarnoise is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Posts: 32 guitarnoise User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Wil,
I don't think there is anything wrong with the level of help you've given me. It's been great and I appreciate it.
I'm not a full time programmer and the php manuals are not written in a way that is easy for beginners to figure them out easily. That's basically why I came here looking for examples. Believe me, I did spend more than 2 or 3 hours trying to understand arrays. I'm not a slow learner, but not a computer professional either. It'll sink in eventually.

Reply With Quote
  #12  
Old June 10th, 2003, 07:04 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
What about using the modulus (%) operator to get it done?

I've been working on something similar, and I was having one hell of a time trying to figure this out as well... Mind you, the 14 hour days wasn't helping my brain-dead self! LOL

Try this... You'll have to modify it to suit your code, but here's exactly what you're looking for:

Code:
echo("<table border='1'><tr>");
     for($i = 0; $i < 10; $i++){
         if (($i % 2) == 0) echo("</tr><tr>");
         
             echo("<td>$i</td>");
     }
     echo("</table>");


Copy and paste that into a file, and run it on your server... Hopefully this is what you're after!

If not, let me know, and we'll figure this out!
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #13  
Old June 10th, 2003, 11:27 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
That looks like a pretty good solutions, however, this solution actually works dynamically no matter how many columns you want the info to span.

http://www.laidbak.net/columnsplit/

Reply With Quote
  #14  
Old June 12th, 2003, 06:05 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
laidbak,

Oh... The 10 is just a value I hard-coded... You could of course take the number of rows retrieved from the DB and divide it by two... Then you'll get your column count in the FOR loop...

It's a simple solution, but it works well...

Reply With Quote
  #15  
Old June 12th, 2003, 06:57 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Quite true... it is a working solution.
Modifying the number of items and factoring it out by 2 shouldn't take more then a second... good job.

Reply With Quote
  #16