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>";
Related
How do I solve this problem to print out matrix which highlights the first row as image
Using for loop PHP to print matrix 10*10 in table form. Note the first column will color with pink
as image
example
Try this code
<?php
echo "<table>";
for($x=1;$x<=10;$x++){
echo "<tr>";
for($y=1;$y<=10;$y++){
echo "<td>".$x*$y."</td>";
}
echo "</tr>";
}
echo "</table>";
To achive this we will check if its first row and the first column in nested for loop. Just see the whole there is not much to explain here.
WHOLE CODE
echo "<table>";
for($x=1;$x<=10;$x++){
echo "<tr>";
if($x == 1){
echo "<td></td>";
for($i = 1; $i <= 10; $i++){
echo "<td style='background-color:blue;'>".$i ."</td>";
}
echo "</tr><tr>";
}
for($y=1;$y<=10;$y++){
if($y == 1){
echo "<td style='background-color:blue;'>".$x ."</td>";
}
echo "<td>".$x*$y."</td>";
}
echo "</tr>";
}
echo "</table>";
OUTPUT
You can just edit the design. since you didn't post your css and html code.
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
I want to display 10 rows per page.If the data present in the table it is display now.Two records in the table that two records are display now.But I want to display the 8 empty row in the table.I have some confusion with this concept.
Now the screen shot display two records which is in db.With this I want to display 8 more empty rows.
while($fet=mysql_fetch_assoc($sql1)) {
$id=$fet['c_id'];
$address=$fet['address'];
$chk=$fet["c_name"];
if($chk!='') {
echo "<tr>";
echo "<td style='align:center'><a href='client_view.php?cid=".$fet["c_id"]."'>".$fet["c_name"]."</a></td>";
echo "<td style='align:center'><a class='ima' href='client_details.php?cid=".$fet["c_id"]."'><img src='image/edit1.png' alt='edit' style='width:20px; height:20px' title=Edit></a></td><td style='align:center'>
<a class='ima' href='clients.php?del=".$fet["c_id"]."'><img src='image/delete1.png' alt='delete' style='width:20px;height:20px' title=Delete></a></td>";
echo "</tr>";
} else {
echo "<tr><td><\td><td></td></tr>";
}
}
Count rows you found by SQL query and then put there empty rows, if needed.
while($fet=mysql_fetch_assoc($sql1)) {
...
}
if (mysql_num_rows($sql1) < 10) {
$empty_rows = 10 - mysql_num_rows($sql1);
for ($i = 0; $i < $empty_rows; $i++) {
echo '<tr><td><td>';
}
}
Try this:
$i=0;
while($fet=mysql_fetch_assoc($sql1)){
$i++;
echo "<tr>";
echo "<td style='align:center'><a href='client_view.php?cid=".$fet["c_id"]."'>".$fet["c_name"]."</a></td>";
echo "<td style='align:center'><a class='ima' href='client_details.php?cid=".$fet["c_id"]."'><img src='image/edit1.png' alt='edit' style='width:20px; height:20px' title=Edit></a></td><td style='align:center'><a class='ima' href='clients.php?del=".$fet["c_id"]."'><img src='image/delete1.png' alt='delete' style='width:20px;height:20px' title=Delete></a></td>";
echo "</tr>";
}
while ($i < 10) {
$i++;
echo "<tr><td></td><td></td></tr>";
}
I am using the following code to get results and want to have it displayed in two rows horizontially.
echo "<table border='0' width='700px' align='center'>";
echo "<tr>";
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
**echo "</tr><tr>";**
echo "<td class='search'>" . $number['count'] . "</td>";
}
echo "<tr>";
echo "</table>";
Hence I need this part of the code not to be included in the loop echo "";
Please help, this must be really simple.
I want the results to be displayed 10 across as I limit my query to
the top 10.
So it would be:
Number Number Number etc. Count Count Count etc.
Because the tables need to be drawn one row at a time, you could store all of your number values in one array and all of your count values in a second array, then loop through each array as you're building your rows. So first get your values from the database:
// let's get a variable with the total number of records, i.e. horizontal cells:
$totalRecords = 0;
// get values from db:
while($number = mysqli_fetch_array($result2))
{
$numberArr[] = $number['number'];
$countArr[] = $number['count'];
$totalRecords++;
}
Then in another set of loops render the table (one loop for each row):
// first row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i) // you could also use count($numberArr) instead of $totalRecords
{
echo "<td class='ball_p'>" . $numberArr[$i] . "</td>";
}
echo "</tr>";
// second row
echo "<tr>";
for ($i = 0; $i < $totalRecords; ++$i)
{
echo "<td class='ball_p'>" . $countArr[$i] . "</td>";
}
echo "</tr>";
What about this:
$i = 0;
while($number = mysqli_fetch_array($result2))
{
echo "<td class='ball_p'>" . $number['number'] . "</td>";
while($i++ >= 1)
{
echo "</tr><tr>";
$i = 0;
}
echo "<td class='search'>" . $number['count'] . "</td>";
}
I haven't tested it but the ++ after the $i in the second while loop should increment it after the evaluation which should skip the </tr><tr> the first time and print it the second.
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>";