473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Working with an array and HTML

60
I want to "put" the values of an array in a <table>. The catch is that i have to arrange the table in a such a way that the number of columns is variable.

I tried with whiles, foreach but with no luck. Then i thought using count() and intval() to get the integer number of how many values should go per column, but i don't know how to continue....

Some help ??? (if possible not the solution)
May 30 '07 #1
8 1510
Can you give some more details? I'm sure it's possible, but it's a bit tricky to say how without more details.
May 31 '07 #2
pplers
60
which details do you want me to give ???
Jun 1 '07 #3
Motoma
3,237 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. echo '<table>';
  2. foreach($arr as $row)
  3. {
  4.   echo '<tr>';
  5.   foreach($row as $column) echo "<td>$column</td>";
  6.   echo '</tr>';
  7. }
  8. echo '</table>';
  9.  
If that does not help, you will need to give us more information.
Jun 1 '07 #4
pplers
60
The problem with your script is that $row isn't an array so it will show an error the script..
Jun 4 '07 #5
henryrhenryr
103 100+
[PHP]
echo '<table>';
foreach($arr as $row)
{
if (is_array($row) && !empty($row) {
echo '<tr>';
foreach($row as $column) echo "<td>$column</td>";
echo '</tr>';
}
}
echo '</table>';
[/PHP]

You are very mysterious in your requests. If you posted the array you are using it would be easier to post a useful response. I adapted the previous response simply to check if $row is an array. That way the row is only added if there is something inside it.

You may also have a varying number of columns. In which case I suggest two options. 1. if you know the maximum, then use something like inside the first foreach loop:

[PHP]
$c=0; //counter
$m=5; //max number of cols
foreach ($row as $column) {
$c++;
//echo the row inside conditional as above
}
if ($c<$m) {
for ($i=$c+1;$c<=$m;$i++) {
echo '<td>&nbsp;</td>';
}
}
[/PHP]

If you don't know $m (ie you might have more cols or less depending on data), then you must determine it in advance by looping through the array and counting the sub-array (ie $m=count($row)). Bit heavy handed I suppose.
Jun 4 '07 #6
Motoma
3,237 Expert 2GB
The problem with your script is that $row isn't an array so it will show an error the script..
I think you missed the foreach($arr as $row) section of the code. That small segment of code will iterate through any two dimensional array and build a table from it.
Jun 4 '07 #7
pplers
60
You got an array, $a = array('asd','a8sy','af9','dsfk','35','4w6','spn',' 9eit',...........) with a large number of elements. And also there's a form with a only one textbox for the user to introduce a number.The elements of the array $a will have to be arranged in N columns (N = number introduced by user).

OK ????
Jun 4 '07 #8
pbmods
5,821 Expert 4TB
So you want the number of columns to be set by the User, but you want to make sure that every element in the array gets output. Is this correct?

Consider this:

If you were to use a counter to keep track of how many elements you've output, how could you keep track of how many columns you've output? How about modulus?

If (($counter % $_GET['nCols']) === 0), then you should precede your output with a '</tr><tr>'.
Jun 4 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Amy Kimber | last post by:
Hello, I have a file upload page, and I've had it working fine, it was beautiful :-) Anyway, the powers that be moved hosts... and it doesn't work now. The file name is correct, the directory...
0
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. ...
1
by: Rob | last post by:
Access Gurus, This script was used to view the reports on the web by choosing a date on or before the current day.Recently it stopped working and i am not able to figure where the problem is. ...
7
by: Steve Wright | last post by:
Forgive my ignorance, but I am just starting to learn about sessions in PHP and how to pass data from one page to the next. What I'm about to explain could just be my misunderstanding of how...
2
by: fedorpawel | last post by:
I want to redirect the user to the current month such as November2006.html, but only if the month exists in pubMon array. This code works correctely in IE but stopped working in the new Firefox. In...
7
by: Ja NE | last post by:
(as first - thank you for all your help several years ago, and second - please apologise me for my clumsy enlgish) so, preface: some users on my photo related site sometimes enter (link) giant...
10
by: AC | last post by:
I had a page that does some event setup on window.onload: function prepEvents() { document.getElementById("menumap_sales").onmouseover = swapMenuSales; // etc } window.onload = prepEvents;
9
by: mrcheeky | last post by:
Hi, I'm stuck, but it's almost working! From a html page, my javascript calls a server-side php script. The php reads a value from a server-side .txt file and passes it back as a javascript...
3
by: PrabodhanP | last post by:
I have CSS based mouseover scrolling for divContent embeded in my webpage.It works fine in IE,but not working in mozilla-FF. It is located at the location.....
2
by: urbanedge | last post by:
I've uploaded a site to a new server and now the headers in the mail function are not working to send the required email response. This is a newly acquired site and my php knowledge is at the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.