|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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:
|
|
#7
|
|||
|
|||
|
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?
|
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
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. ![]() |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
|||
|
|||
|
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. |
|
#12
|
|||
|
|||
|
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 |
|
#13
|
|||
|
|||
|
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/ |
|
#14
|
|||
|
|||
|
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... |
|
#15
|
|||
|
|||
|
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. ![]() |
|
#16
|