I'm trying to rotate an array (5x6 for example) on pos to right each value, the last must to be the first
//ADD VALUES (RANDOM)
for($i=0;$i<6;$i++){
for($j=0;$j<7;$j++){
$aleatorio=mt_rand(10,99);
$a[$i][$j]=$aleatorio;
}
}
echo "<br>"; / SHOW IN A TABLE
echo "<table border=1 width=200px >";
for($i=0;$i<count($a);$i++){
echo "<tr>";
for($j=0;$j<count($a[0]);$j++){
echo "<td>".$a[$i][$j]."</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<br>";echo "<br>";
$last=array_pop($a); //KEEP THE LAST VALUE
array_unshift($a,$last); //PUT IN THE FIRST POSITION
//SHOW AGAIN
echo "<br>";echo "<br>";
echo "<table border=1 width=200px >";
for($i=0;$i<count($a);$i++){
echo "<tr>";
for($j=0;$j<count($a[0]);$j++){
echo "<td>".$a[$i][$j]."</td>";
}
echo "</tr>";
}
echo "</table>";
Why does the last value appear in first row last column? not in [0][0] position
Related
Following is the code
I want out put as
http://crysol.com/crysol_soft/Test/Screenshot_3.png
With following code I am getting output as
http://crysol.com/crysol_soft/Test/Screenshot_4.png
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
echo " </tr>";
}
echo "<td rowspan='10'>Good</td>";
?>
What are the changes required
Here is the code with your desired output
used if condition for print only 1 time good column and used rowspan='5'
<?php
echo "<table border='1'>";
echo "<th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th>";
echo '<tr>';
echo "<td rowspan='5'>Cat</td>";
for($i=1;$i<=5;$i++){
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
?>
and also you can used this code
for($i=1;$i<=5;$i++){
echo '<tr>';
if($i==1){
echo "<td rowspan='5'>Cat</td>";
}
echo '<td>'.$i.'</td>';
if($i==1){
echo "<td rowspan='5'>Good</td>";
}
echo " </tr>";
}
Create Table inside td:
<?php
echo "<table border='1'>";
echo "<tr><th>Name</th>";
echo "<th>Number</th>";
echo "<th>Status</th></tr>";
echo "<tr><td style=''>Cat</td>";
echo "<td><table style='width:100%;'>";
for($i=1;$i<=5;$i++){
echo "<tr>";
echo "<td style='border-bottom:1pt solid black;'>".$i.'</td>';
echo "</tr>";
}
echo "</table></td>";
echo "<td>Good</td></tr>";
?>
I want to write the rows as many as the number stated in $row. (instead of one row only). How can I achieve this? What am I doing wrong? thanks.
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++)
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
echo "</table>";
echo $i+1;
}
?>
You are closing table inside the loop. Change to the following
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++){
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
}
echo "</table>";
?>
try this
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++)
{
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
}
echo "</table>";
?>
try this code i have added some mistake done by you
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++){ //add bracket here
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
//echo $i+1; //remove this one
}
echo "</table>"; //close table tag outside the loop
?>
I'm attempting to display my data in a table from my database, I used the exact same code for another table but for some reason the table appears but is empty.
Thanks its solved!
you have to put brackets in while loop, close tag and the last :
echo "<table border=1 cellpadding=5>";
echo "<tr>";
echo "<th>Workshop Name</th>";
echo "<th>Workshop Price (£)</th>";
echo "<th>Workshop Description</th>";
echo "<th>Further Information</th>";
echo "</tr>";
$SQL="select workshopId,workshopName, workshopPrice,workshopDescription from Workshops order by workshopId";
$exeSQL=mysql_query($SQL) or die (mysql_error());
while ($arrayworkshop=mysql_fetch_array($exeSQL))
{
echo "<tr>";
//display details
echo "<td>".$arrayworkshop['workshopName']."</td>";
echo "<td>".$arrayworkshop['workshopPrice']."</td>";
echo "<td>".$arrayworkshop['workshopDescription']."</td>";
echo "<td><a href='ViewWorkshop.php?workshopId=".$arrayworkshop['workshopId']."'>";
//Display name of the Task
echo "Further Information"."</a></td>";
echo "</tr>";
}
echo "</table>";
I would like to insert a new random image in a row (all by itself) after 10 rows of data have been populated.
I'm thinking I should input something like this:
if($counter % 10 == 0) {
echo 'image file';
}
But not really sure how to incorporate this in my code:
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td>";
echo "<td><H3>First Name</H3></td> <td><H3>Last Name</H3></td>";
echo "<td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter
$count=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter equals
$count=$count+1;
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td> <H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter
$count=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter equals
$count++;
//insert an image every 10 rows
if($count==10){
$count=0;
echo 'yourimage.jpg';
}
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";
if($counter % 10 == 0) {
echo 'image file';
}
10, not 100 :). Put this after your $count=$count+1 statement in a td colspan=however many columns you have.
Thanks to the help of Here is the completed code that works (however, I still can't get the random image to show, but I can get an image :-)
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td><H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter for data
$count=0;
//declaring counter for random image
$counter_for_image=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter for actual count
$count=$count+1;
//counter for image count
//so I can reset count and
//not affect actual count
$counter_for_image++;
/* does not work
// start random image code
$images = array(
0 => '1.jpg',
1 => '2.jpg',
);
$image = $images[ rand(0,(count($images)-1)) ];
$randomimage = "<img src=\"/http://wwww.my site.com/storm/images/".$image."\" alt=\"\" border=\"0\" />";
//print($output);
// end random image code
*/
// start every 10th row image display code
if($counter_for_image==10){
$counter_for_image=0;
echo '<tr><td colspan="6"><center><img src="http://www.mysite.com/storm/images/eric.jpg"/></center></td></tr>';
/* would use this if I could get random image to work
echo '<tr><td colspan="6"><center>';
print($randomimage);
echo '</center></td></tr>';
*/
} // end every 10th row image display code
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";
I've put together the following code which creates a table upon a selection being made with a drop down menu.
echo "<table>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The problem I've got is that for each record that is returned the 'headers' are repeated. The live page can be found here. I just wonder whether someone could perhaps take a look at this please and tell me where I've gone wrong.
Apologies for the really simple question, but I've been looking at this for a while and I just can't find the answer. I think it just needs a fresh pair of eyes to look at it.
Try this, you just need to get headers out of while loop
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
The answer is obvious, you are repeating the output of the headers in the loop. Move the
while($rows=mysql_fetch_array($result)){
after the first
echo "</tr>";
You need to put the headers outside of the while loop:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result = mysql_query($query);
while ($rows = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $rows['findname'] . "</td>";
echo "<td>" . $rows['finddescription'] . "</td>";
echo "</tr>";
}
echo "</table>";
This should work:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Your headers are being repeated because you are writing them inside the loop, for each row returned by the query. You simply need to move your headers outside the loop so they're only written once, before the rows returned by the query start printing:
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
headers are repeating becasue they are in while loop, it should work well
echo "<table>";
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";
Change to:
echo "<tr>";
echo "<th>Find Name:</th>";
echo "<th>Find Description:</th>";
echo "</tr>";
echo "<tr>";
while($rows=mysql_fetch_array($result)){
echo "<td>".$rows['findname']."</td>";
echo "<td>".$rows['finddescription']."</td>";
echo "</tr>";
}
echo "</table>";