How can i echo image size in PHP? - php

I try to put image size in image src tag, but it still does not work. The image can be displayed fine.
<?php
$files = glob("uploads/*.*");
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 4; $row++) {
echo "<tr> \n";
for ($col=1; $col<=4; $col++) {
$f=$f+1;
$getfile = $files[$f];
echo "<td>";
echo "<img src=$getfile > ";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";

You can use getimagesize() function http://php.net/manual/en/function.getimagesize.php to fetch image size
<?php
$files = glob("uploads/*.*");
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 4; $row++) {
echo "<tr> \n";
for ($col=1; $col<=4; $col++) {
$f=$f+1;
$getfile = $files[$f];
$size = getimagesize($getfile);
echo "<td>";
echo "<img src=$getfile $size[3]> ";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>

PHP Function filesize will hep you here
echo filesize($filename) . ' bytes';
http://php.net/manual/en/function.filesize.php

Related

PHP: Highlight every second row of table

I use this code to create a HTML table
for ($x = 0; $x <= $requiredRows; $x++) {
echo "<tr>";
echo "<td>";
echo someArrayStuff($x);
echo "</td>";
echo "<td>";
echo $someArrayStuff[$x][arrayinfos];
echo "</td>";
echo "<td>";
echo $someArrayStuff[$x][arrayinfos];
echo "</td>";
echo "</tr>";
}
Now I want to highlight every second row with a background color, how can I do this with PHP? I don't want to define it in the CSS file.
Thanks for all your help, now it works with this code:
if($x%2 == 0) { echo "<tr bgcolor='#FFFFF'>"; } else { echo "<tr bgcolor='#FFFFF'>"; }
if($x%2 == 0) {
echo '<tr class="bg-highlight">';
} else {
echo "<tr>";
}
and define a css class:
.bg-highlight {
background-color: red;
}
You can use mod. $x%2 will check if its a even row. which will be alternate row. If its found as even row then a different background color is given.
for ($x = 0; $x <= $requiredRows; $x++) {
if($x%2 == 0) { //even
echo "<tr style="background-color: #FF0000">";
} else { // odd
echo "<tr>";
}
echo "<td>";
echo someArrayStuff($x);
echo "</td>";
echo "<td>";
echo $someArrayStuff[$x][arrayinfos];
echo "</td>";
echo "<td>";
echo $someArrayStuff[$x][arrayinfos];
echo "</td>";
echo "</tr>";
}
You can simply create a dynamic class which will alter as per your loop
$class = ($x%2 == 0) ? "highlight" : "";
echo "<tr class='$class'>";
Just create a class named
.highlight{
background-color: grey;
}
Or You can simply use this code in css for odd or even rows of table.
div:nth-child(even)
{
background-color: yellow;
}
for ($x = 0; $x <= $requiredRows; $x++) {
echo "<tr";
if ($x % 2 === 1) {echo " style=\"background-color: yourFavoriteColor;\"";}
echo ">";
..................

Highlight first row in php while loop

I'm getting all rows from mysql database. Now I want to highlight only first row in php while loop with class name keywordHighlight.
How do I highlight only first row in php while loop result ?
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($resGetSearch = mysql_fetch_array($getSearch)){
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
echo "<tr onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Do it like this
$i = 1;
while($resGetSearch = mysql_fetch_array($getSearch)){
$highlight = $i == 1 ? 'keywordHighlight' : '';
echo "<tr class='{$highlight}' onclick='getDetails($SearchCdid);' >";
---------------
-------------
-------------
$i++;
}
Only with CSS
or you can highlight it only with css
#highlight tbody tr:nth-child(1){
background: #ff6600;
}
There is more and elegant way to highlight only first row with only css not need to code, consider the example http://jsbin.com/soravuzakahu/1/
You could just add a boolean outside the loop. Like so:
$first = true;
while($resGetSearch = mysql_fetch_array($getSearch)){
if(first == true){
// Add code here that only applies to the first iteration.
}
$first = false;
}
<?php
if($numSearch > 0){
echo "<font color='green'>We found $numSearch result(s).</font>";
echo "<table width='100%' cellpadding='0' cellspacing='0'>";
echo "<thead>";
echo "<tr>";
echo "<td class='' valign='top' width='200'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "<td class='' valign='top' width='125'></td>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
$i = 0;
while($resGetSearch = mysql_fetch_array($getSearch)){
++$i;
$SearchCdid = $resGetSearch['cdid'];
$SearchFamilyName = $resGetSearch['family_name'];
$SearchGivenName = $resGetSearch['given_name'];
$SearchCompamyCid = $resGetSearch['cid'];
$SearchDepartment = $resGetSearch['department'];
$SearchTitle = $resGetSearch['title'];
$SearchComapnyName = mysql_query("SELECT company_name FROM company WHERE cid = '$SearchCompamyCid' ");
$resSearchCompanyName = mysql_fetch_array($SearchComapnyName);
$companyName = $resSearchCompanyName['company_name'];
if (strlen($companyName) >= 20) {
$companyName = substr($companyName, 0, 10). "" . substr($companyName, -5);
}else{
$companyName = $companyName;
}
// my Highlighted class = keywordHighlight";
if($i == 1)
echo "<tr onclick='getDetails($SearchCdid);' >";
else
echo "<tr class='keywordHighlight' onclick='getDetails($SearchCdid);' >";
echo "<td valign='top' >$companyName</td>";
echo "<td valign='top'>$SearchFamilyName</td>";
echo "<td valign='top'>$SearchGivenName</td>";
echo "<td valign='top'>$SearchDepartment</td>";
echo "<td valign='top'>$SearchTitle</td>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr/>";
echo "<br/>";
}//elseif is not empty search
elseif($numSearch === 0){
echo "<font color='red'>No Matches.</font>";
}
Use a flag and set its value to true and while in loop check its value , if its true then print class name and set its value to false. Like $flag=true; then inside while loop check
if($flag==true) {
<td class='yourclassname';
$flag= false;
}
put some flag $f=0;
if f==0 then do something like this:
$highlight="<div class='keywordHighlight'>valur of first row</div>";// instead of dive you can use table also it depends on how you want to display.
$f=$f+1;
and rest in else part.
$first = true;
while ( $resGetSearch = mysql_fetch_array($getSearch) )
{
if ( $first == true )
{
// Add code here that only applies to the first iteration.
$first = false;
}
else
{
// Add code here that only applies to the 2, 3, and so on.
}
}

Split a table into three rows

I've got a PHP page, some of which is something like this:
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th>
</tr>";
while($row = mysql_fetch_array($raw_results))
{
echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
echo "</tr>";
}
echo "</table>";
How can I change this so the table goes C1 C1 C1?
So if I have the following set of values - 1 2 3 4 5 6 7 8 9 - the table returns the following
C1 C1 C1
1 2 3
4 5 6
7 8 9
How can I do this?
something like this:
$cpt=1;
echo "<tr>";
while($row = mysql_fetch_array($raw_results))
{
echo "<td>" . $row['C1'] . "</td>";
if ($cpt%3==0)
echo "</tr><tr>";
$cpt++;
}
echo "</tr>";
will create a new row after every 3 result
Define in $N how many cols you want.
$N=3;
$col=0;
while($row = mysql_fetch_array($raw_results))
{
$col++;
if ($col==1) echo "<tr>";
echo "<td>" . $row['C1'] . "</td>";
if ($col==$N)
{
$col=0;
echo "</tr>";
}
}
if ($col>0) //if there is an open <tr>...
echo "</tr>";
if(mysql_num_rows($raw_results) > 0) {
echo "<table border='1'>
<tr><th>C1</th><th>C1</th><th>C1</th></tr>";
$col = 0;
while($row = mysql_fetch_array($raw_results)) {
if ($col == 0) { echo "<tr>"; }
echo "<td>" . $row['C1'] . "</td>";
if ($col == 2) { echo "</tr>"; }
$col = ($col + 1) % 3
}
echo "</table>";
}
This is also Working (little bit short one)
$a=1;
while($row = mysql_fetch_array($raw_results))
{
echo "<td>".$row['c1']."</td>".(($a%3==0)?'</tr><tr>':'');
$a++;
}
Change it to
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th><th>C1</th><th>C1</th>";
$x = 0;
while($row = mysql_fetch_array($raw_results))
{
if ($x % 3 == 0) {
echo "</tr><tr>";
}
echo "<td>" . $row['C1'] . "</td>";
$x++;
}
echo "</table>";
I assume you have just 3 columns and count of your result is submultiple of 3
pay attention to </tr><tr>
No test so it might be some syntax error but you can do something like this:
if(mysql_num_rows($raw_results) > 0){
echo "<table border='1'>
<tr>
<th>C1</th>
<th>C1</th>
<th>C1</th>
</tr><tr>";
$i = 0;
while($row = mysql_fetch_array($raw_results))
{
if ($i == 2) {
$i = 0;
echo "</tr>";
echo "<tr>";
}
echo "<td>" . $row['C1'] . "</td>";
$i++;
}
if ($i > 0) {
echo "</tr>";
}
echo "</table>";

How to display 2 variables in a single cell Mysql

I would like to display two picture variables in a single cell
http://i.imgur.com/9ZuRhnD.gif
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n";
}
echo "<td align='center' width='140'>" . "<img src=\"{$row['Limgt']}\">" ."</td>";
echo "<td align='center' width='40'>" . "<img src=\"{$row['Limg']}\">" ."</td>";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
What am I missing
You're putting them each in their own cell () - try this:
$result = mysqli_query($query)
or die(mysql_error());
if (mysqli_num_rows($result) > 0) {
echo "<table border='0' style='border-collapse: collapse;border-color: white;'>";
$i = 0;
while($row = mysqli_fetch_array($result)){
$Limg = $row['Limg'];
$Limgt = $row['Limgt'];
if ($i==0) {
echo "<tr>\n<td>";
}
echo "<img src=\"{$row['Limgt']}\">";
echo "<img src=\"{$row['Limg']}\">";
$i++;
if ($i == $items) {
echo "</td></tr>\n";
$i = 0;
}
}
echo '</table>';
}else {
echo 'no records found';
}
?>
you are making them in two TDs so you have to make them in one TD like that
echo "<td align='center' width='140'>
<img src=\"{$row['Limgt']}\">
<img src=\"{$row['Limg']}\">
</td>";

PHP loop - creating a table using a loop and giving it headers

I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP

Categories