i have array of 10 item and each item has own price.
i have to select some item and display the selected item's average and if any of selected item's above the average i have to display it's pic in 2x2 table different pic in each cell.
i write this code but the problem is the pic repeat in the 4 cell's:
<?php
echo "<table border=2>";
$incVar = 0;
for ($x = 0; $x < 2; $x++) {
echo"<tr>";
for ($y = 0; $y < 2; $y++) {
echo"<td>";
while($incVar <=9){
if (isset($camera[$incVar]) && $camera[$incVar] >= $avreag){
echo '<img src="' . $pic[$incVar] . '" width="200" height="200">';
}
$incVar++;
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
first you have to create image array because if you will display in this manner and if some items does not match the condition then it will leave blank <td>
solution :
<?php
echo "<table border=2>";
$incVar = 0;
for ($x = 0; $x < 2; $x++) {
echo"<tr>";
for ($y = 0; $y < 2; $y++) {
echo"<td>";
if (isset($camera[$incVar]) && $camera[$incVar] >= $avreag) {
echo '<img src="' . $pic[$incVar] . '" width="200" height="200">';
}
$incVar++;
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
no you need to display $imagesArr in 4x4 for loop
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
Hi i have this PHP which take sql query divide it into two columns and print table. Now i need to change color of TD where value > 0. I've add color change by class. It's working but not correct. It change color of whole string but not TD cell.
$stmt=ociparse($olink, $sql);
if (!$stmt) {
$e = oci_error($olink);
trigger_error(htmlentities($e['message']), E_USER_ERROR);
}
$r =ociexecute($stmt,OCI_NO_AUTO_COMMIT);
if (!$r) {
$e = oci_error($stmt); // For oci_execute errors pass the statement handle
print htmlentities($e['message']);
print "\n<pre>\n";
print htmlentities($e['sqltext']);
printf("\n%".($e['offset']+1)."s", "^");
print "\n</pre>\n";
}
$ncols = oci_num_fields($stmt);
$cur_dt = 1;
echo "<TABLE border=\"1\" width=\"100%\" align='center' cellspacing='0' cellpadding='0' class=\"sortable\">";
/* echo "\n<tr>"; */
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<th bgcolor=\"#2B75C1\" class=\"header\">".oci_field_name($stmt, $i)."</th>";
}
$str=1;
while (oci_fetch($stmt)) {
if ($str % 2 == 1)
{
echo "\n";
echo "<tr";
$hrr="";
echo ">";
}
for ($i = 1; $i <= $ncols; $i++) {
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
echo "</TABLE>";
oci_close($olink);
?>
</div>
</body>
</html>
0 in second column should be blue, but it ged "hour" class and change color
create a class in your css file like .change{background-color:green} in your
<td>
for ($i = 1; $i <= $ncols; $i++) {
$hrr ="";
if (oci_result($stmt, 2) >= $cur_dt) {$hrr = 'class="change"';}
echo "<td ".$hrr.">";
echo oci_result($stmt, $i);
}
echo "</td>";
if ($str % 2 == 0) {echo "</tr> \n";}
$str++;
}
Use css to style a single cell style="background-color: #2B75C1;"
If you want to change the color of the string style="color: #2B75C1;"
echo '<th style="background-color: #2B75C1;" class=\"header\">'.oci_field_name($stmt, $i).'</th>';
Replace
echo "<td";
echo $hrr;
if (oci_result($stmt, 2) >= $cur_dt) {$hrr= " class=\"hour\"";echo $hrr;}
echo ">";
echo oci_result($stmt, $i);
}
echo "</td>";
For
echo '<td' . (oci_result($stmt, 2) >= $cur_dt ? ' class="hour"' : '') . '>';
// ^ shouldn't be there '$i'?
echo oci_result($stmt, $i);
echo '</td>'; // closing tag for <td> in optional, you can remove that to make your HTML more readable.
// remove $hrr variable, you don't need it.
I think, you have to replace the "2" with $i . Replace the following code
if (oci_result($stmt, 2) >= $cur_dt)
With the solution.
Solution 1 :
if (oci_result($stmt, $i ) >= $cur_dt)
Solution 2 :
if ( (oci_result($stmt, $i ) >= $cur_dt) || oci_result($stmt, $i ) > 0)
I tried many hours to simplify this code:
<?
echo '<div class="eme3-left">'."\n".'<table>'."\n";
for ($n=1,$i=10; $n<=100,$i<=100;$n+=10,$i+=10)
{
echo '<tr>
<td class="dick-grau">'.$n.' → '.$i.'</td>
<td>'.$n.' = '.decbin($n).'<sub>2</sub></td>
<td>'.($n+1).' = '.decbin($n+1).'<sub>2</sub></td>
<td>'.($n+2).' = '.decbin($n+2).'<sub>2</sub></td>
<td>'.($n+3).' = '.decbin($n+3).'<sub>2</sub></td>
<td>'.($n+4).' = '.decbin($n+4).'<sub>2</sub></td>
<td>'.($n+5).' = '.decbin($n+5).'<sub>2</sub></td>
<td>'.($n+6).' = '.decbin($n+6).'<sub>2</sub></td>
<td>'.($n+7).' = '.decbin($n+7).'<sub>2</sub></td>
<td>'.($n+8).' = '.decbin($n+8).'<sub>2</sub></td>
<td>'.($n+9).' = '.decbin($n+9).'<sub>2</sub></td>';
};
echo '</tr></table>'."\n".'</div>';
?>
How to add the if condition from the third code for the second for loop ?
<?php
echo '<div class="eme3-left">'."\n".'<table>'."\n";
for ($n=1,$i=10; $n<=100,$i<=100; $n+=10, $i+=10) {
echo '<TR>'."\n".'<td class="dick-grau">'.$n.' → '.$i.'</td>';
for ($t = 1; $t<=10; $t++) {
echo '<td>'.$t.' = '.decbin($t).'<sub>2</sub></td>'."\n";
}
echo "</tr>";
}
echo '</table>'."\n".'</div>';
?>
In another version the following loop works perfectly, but when I add it to the code above, the if-condition don't work.
for($i=1;$i<=100;$i++){
echo '<td>'.$i.' = '.decbin($i).'<sub>2</sub></td>'."\n";
if($i%10 == 0)
echo '</tr>'."\n";
}
Maybe their are more suggestions for improvements.
This is not a complete example but should get you there.
<?php
$ints = range(1,100);
$ints_grouped_by_10 = array_chunk($ints,10);
foreach($ints_grouped_by_10 as $int_group){
echo "<tr>";
echo '<td class="dick-grau">'.reset($int_group).' → '.end($int_group).'</td>';
foreach($int_group as $int){
echo "<td>{$int} = ".decbin($int)."</td>";
}
echo "</tr>";
}
?>
[Edit] added the legend (1 -> 10, 11 -> 20)
echo '<div class="eme3-left">'."\n".'<table>'."\n";
for ( $n=1 ; $n <= 100 ; $n+=10 )
{
echo '<tr>
<td class="dick-grau">'.$n.' → '.($n+9).'</td>';
for ( $j = 0 ; $j < 10 ; $j++ )
echo '<td>'.($n + $j).' = '.decbin($n+$j).'<sub>2</sub></td>';
echo '<tr>';
}
echo '</table>'."\n".'</div>';
I want to show 4 images from database in a table (2*2) I know how to do that. But I have also special condition (if there are less picture than 4, it will show default pictures to show 4 images totally)
I manage this in a while loop. If there are 4 images there is no problem, it works as I want but when there are fewer images (which means default images will be completed 4 images in total) it doesn't work. I can't figure out how to do that.
Any help?
<table>
<tr>
<?php
$todisplay = 4;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT 0,4;");
while ($row = mysql_fetch_array($query)) {
$x++;
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
$displayed_number++;
if ($x % 2==0) {
echo "</tr><tr>";}
}
echo str_repeat("<td>
<img src='images/png/defthumb.png'> </td>", $todisplay-$displayed_number);
?>
</tr></table>
You were not far from it - simply create another loop that does the same thing, but with the default image instead. Also, the $displayed_number seems to hold the same value as $x, so I deleted it.
<table>
<tr>
<?php
$todisplay = 4;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT 0,4;");
$x = 0;
while ($row = mysql_fetch_array($query)) {
$x++;
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
if ($x % 2==0) {
echo "</tr><tr>";}
}
while ( $x < $todisplay) {
$x++;
echo "<td><img src='images/png/defthumb.png'/></td>";
if ($x % 2==0) {
echo "</tr><tr>";}
}
?>
</tr></table>
Instead of looping over the rows returned by your query, why not just loop four times and attempt to fetch a row instead?
<table>
<tr>
<?php
$tablerows = 2;
$query = mysql_query("select * from Images where Country_ID=$co_id LIMIT " + ($tablerows * 2) + ";");
for ($x = 1; $x <= ($tablerows * 2); $x++) {
if ($row = mysql_fetch_array($query)) {
echo "<td><img src='".$row['I1_Th'] . "'/></td>";
} else {
echo "<td><img src='images/png/defthumb.png'></td>";
}
if ($x % 2==0 && $x != $tablerows * 2) {
echo "</tr><tr>";}
}
?>
</tr></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>";