I'd like to fill a table vertically. For example one name on one row, the age on the second row alternately, and limit the table to 4 columns:
Is this possible with this kind of code?
Here is my Code:
<?php
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
echo "<table border=1>";
for($i=0;$i<count($age);$i++)
{
if ($i > 0 && $i % 4 == 0)
{
echo "</tr><tr>";
}
echo "<td>";
echo $name[$i];
echo "</td>";
echo "<td>";
echo $age[$i];
echo "</td>";
}
echo "</tr>";
?>
Please try this. It is not the most elegant way but will give you an idea and maybe improve your answer. I will suggest you maybe use multi-dimensional array to store age and name together.
$age = array("26","16","17","19","24","30");
$name = array("adam","andrew","tim","mike","don","eddy");
$ageChunks = array_chunk($age, 4); //divide age array into chunks of 4
$nameChunks = array_chunk($name, 4); //divide name array into chunks of 4
//print table data
function printData($td)
{
echo "<td> ".$td." <td>";
}
//print table row. Assumption here is $nameChunks and $ageChunks are same length.
function printRow($nameChunks, $ageChunks)
{
foreach ($nameChunks as $key=>$val) {
echo "<tr>";
array_map("printData", $nameChunks[$key]);
echo "</tr><tr>";
array_map("printData", $ageChunks[$key]);
echo "</tr>";
}
}
//print table
function printTable($nameChunks, $ageChunks)
{
echo "<table>";
printRow($nameChunks, $ageChunks);
echo "</table>";
}
printTable($nameChunks, $ageChunks);
you need to change condition
if ($i > 0 && $i % 4 == 0) to if ($i > 0 && $i % 2 == 0) because you print 2 columns per iteration
Related
I am having problems printing out a timetable using the results from a SQL statement and some HTML layout with PHP.
I have the times of the classes along the top of the page.
I am trying to put the days of the week along the side of the page and then check if the result of the SQL statement (containing a module) should be printed in the specific day and time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th></th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
while($row = mysqli_fetch_array($result1)) {
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "$printday";
for($t = 9; $t < 17; $t++) {
if($row['Day'] == $d && $row['Time'] == $t){ //fix this area so that it moves along
echo "<td>" . $row['ModuleName'] . "<br/>\n " .$row['Location'] . "</td>";
} //if
else {
echo "<td></td>";
} //else
} //for 2
echo "</tr>";
} //for 1
} //while
The problem is that I am printing out Monday-Friday 3 times as there are 3 $row results. Any idea how I could get this to work.
Your looping through your data in the wrong spot, you first need to put that in an array so you can loop through it for each day/time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th>Day</th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
$courses = array();
while($row = mysqli_fetch_array($result1)) {
$courses[] = $row;
} //while
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "<tr><td>" . $printday . "</td>";
for($t = 9; $t < 17; $t++) {
unset($course_row);
foreach($courses as $course){
if($course['Day'] == $d && $course['Time'] == $t){ //fix this area so that it moves along
$course_row .= $course['ModuleName'] . "<br/>\n " .$course['Location'] . "<br/>\n<br/>\n";
} //if
}
if(isset($course_row)){
echo "<td>" . $course_row . "</td>";
} else {
echo "<td> </td>";
} //else
} //for 2
echo "</tr>";
} //for 1
Created a script that generates a table with 10 rows and 2 cols.
I can't make the cells generate natural numbers nonzero, and divisible by 5 distributed as the following table:
This is my code:
<?php
$rows = 10;
$cols = 2;
echo "<table border='1'>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
//generated cols and rows, now the calculus
if ($tr/5!=0) {
echo "<td>".($tr*5)."</td>";
} else {
echo "<td>".($tr*5)."</td>";
}
}
echo "</tr>";
}
echo "</table>";
?>
Where did I go wrong, how can I make it go from row to row like the image ?
You are making it more complicated than needed. Just add 5 to a counter each cell
Is this what you want?
$rows = 10;
$cols = 2;
echo "<table border='1'>";
$val=0;
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
for($td=1;$td<=$cols;$td++){
$val=$val+5;
echo "<td>".$val."</td>";
}
echo "</tr>";
}
echo "</table>";
I need help rendering data in PHP:
I want to use the results of a SQL query to populate a table, displaying images in a 4-column grid. Here's what I've written so far:
$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
while($row = mysql_fetch_array($result))
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}
<table><tr>
<?$result = mysql_query("SELECT * FROM gallery ORDER BY id ASC ");
$result =mysql_fetch_array($result)
$count=0;
for($row =0; $row<count($result); $row++)
{
$count++
if($count==4){
echo '</tr><tr>';
$count=0;
}
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$row['image_name']."\" width=\"200\" height=\"170\" /></td>";
}
?>
</tr>
</table>
You can do a for loop as was suggested by Frederick:
$num_rows = mysql_num_rows($result);
for($i = 0; $i < $num_rows; $i++)
{
// Check if beginning of row
if($i % 4 == 0)
{
//If not the first row, end the last row first
if($i > 0)
{
echo "</tr>";
}
echo "<tr>";
}
$row = mysql_fetch_assoc($result);
//Output each individual image
echo "<td> {*EACH IMAGE code*}</td>";
}
That should give you the idea.
Try this one:
$arr =array("Test1","Test2","Test3","Test4","Test5","Test6","Test7","Test8","Test9","Test10");
echo "<table><tr>";
for($i = 1 ; $i <= count($arr) ; $i++)
{
echo"<td>".$arr[$i-1]."</td>";
$last = $i % 4 == 0? "<tr/><tr>":"";
echo $last;
}
echo "</table>";
Try this, hope it works for you,
while($row = mysql_fetch_array($result))
{
$data[]= $row['image_name'];## Pass the image data to new array
}
$ImagePerRow = 4;### Set the value of image per table row
echo "<table border='1'><tr>";
foreach($data as $key => $ImageName)
{
if(($key+1) == $ImagePerRow)## if key index+1 is equal to ImagePerRow
{
echo "<td align=\"center\" ><img src=\"upload_gallery/thumbnail/sml_".$ImageName."\" width=\"200\" height=\"170\" /></td></tr><tr>";## then close the table row and start a new table row
$ImagePerRow+=4;### Add a value of 4 in order to increment the imageperRow value on the next loop
}else{
echo "<td align=\"center\" >".$ImageName."</td>";### else echo the normal table data
}
}
echo "</tr></table>";
please i need you help with my script.. I'm trying to echo some content of query into an html table, but i want to break it at every 7th parameter. The script below only breaks the first seven, while the rest are not broken up at interval and they were also echoed out of the html table.
How can I do this please. Thank you for your time and help.
echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;
echo "<tr align= \"center\">\n";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<td>" . "Failed: ";
if ($score_count !== 0) {
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>" . $row8['course_code'] . "</th>";
if ($count == 7) {
echo "</tr>\n";
echo "</table>";
}
}
}
Use modulo operator instead of equality
if ( ($count+1) % 7 ){
The +1 is there so that it doesn't break immediately on $count == 0 because 0%n is 0
You need to use the modulus operator %, which returns the remainder after division. $count % 7 ==0 means the current count is on a multiple of 7, and you should break. You also need to increment $count.
echo "<table class=\"altrowstable\" bgcolor = gold >\n";
$count = 0;
echo "<tr align= \"center\">\n";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<td>"."Failed: ";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo "<th>".$row8['course_code']."</th>";
// Increment $coutn
$count++;
// Check the modulus
if ( $count % 7 == 0 ){
echo "</tr>\n";
echo "</table>";
}
}
}
So basically I have a script that is suppose to display my code into rows of 3, and create a new row. However, it is all displayed in one column. Here's the code.
while($mysql2 = mysql_fetch_array($sql2)) {
$prodlogo = $mysql2['logo'];
$prodid = $mysql2['id'];
$prodname = $mysql2['name'];
$x = 1;
if($x==1) {
echo "<tr>";
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
$x++;
if($x==3) {
echo "</tr>";
$x = 1;
}
}
move the $x out of the loop,otherwise you are reset it to one in every loop
each <td> will be a separate column. I would suggest you format it thus.
while($mysql2 = mysql_fetch_assoc($sql2)) {
foreach($mysql2 as $row) {
echo "<tr>";
echo "<td>".$row['logo']."</td><td>$".row['id']."</td><td>".$row['name']."</td>";
echo "</tr>";
}
}
also you need to use mysql_fetch_assoc() to get an associative array you can access like $row['key']
You may need to instantiate $x = 1 outside of your while loop - you're resetting it to 1 each time you're running it.
Using modulo might be good too:
if (($x % 3) == 0) {
echo "<tr>"
}
echo "<td><img src=images/productpics/$prodlogo></br><a href=viewproduct.php?pid=$prodid>$prodname</a></td>";
if (($x % 3) == 0) {
echo "</tr>"
}
You wouldn't need to worry about resetting $x to 1...