How to call images in a database into 10 images per row? - php

<?php
include 'dbconnect.php';
$query = mysql_query("SELECT * FROM champicons") or die("Error: " . mysql_error());
echo "<table border='10' width='100%' cellpadding='10' >";
echo "<tr>";
while($row = mysql_fetch_array($query)) {
$image = $row[2];
echo "<td>";
echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';
echo "<td>";
}
echo "</tr>";
echo "</table>";
?>
What I'm trying to achieve is for 10 images to be put in 1 row before dropping a row below and making another 10 images appear, right now the more images I put into the database the wider and smaller the current row gets and I can't see all the images.
It's hard to get it to create a new row at 10 images as the images are called on a while loop.

Add a counter before the loop and if in the loop.
$i = 1;
while($row = mysql_fetch_array($query)) {
$image = $row[2];
echo "<td>";
echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';
echo "</td>";
if($i++%10 == 0) echo '</tr><tr>';
}

I think you just need to count your iteration and insert new row according
$i=1;
while($row = mysql_fetch_array($query)){
$image = $row[2];
echo "<td>";
echo '<img src="data:image/png;base64,' . base64_encode($image) . '" />';
echo "</td>";
$i++;
if($i==10){
$i=1;
echo "</tr><tr>";
}
}
As side note your </td> wasn't closed correctly

Related

Display results from DB using Bootstrap

I would like to retrieve my results from my DB in this format using Bootstrap.
Below is my PHP code that I'm currently using, the first entry I want the image to be bigger then the rest.
<?php
$article = mysqli_connect("localhost", "root", "", "blog");
// Check connection
if($article === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM news";
if($result = mysqli_query($article, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<container>";
echo "<row>";
echo "<th>id</th>";
echo "<th>title</th>";
echo "<th>body</th>";
echo "<th>image</th>";
echo "</div>";
while($row = mysqli_fetch_array($result)){
echo "<row>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['body'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</row>";
}
echo "</div>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($article);
}
// Close connection
mysqli_close($article);
?>
I think you should have the condition to check for the first data in your loop to apply a bigger image size.
You can simply add condition:
$resultNum = 1;
while($row = mysqli_fetch_array($result)){
if($resultNum == 1) {
// TODO: show bigger image
} else {
// usual image
echo "<row>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['body'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</row>";
}
$resultNum++;
}
Just make a counter and then do a condition using the first number of the counter
Your code simplyfied:
$query = mysqli_query($article, "SELECT * FROM news");
$n = 1; //start the counter
if(mysqli_num_rows($query) > 0){ //detect if have rows
foreach ($query as $key => $value) {
if($n == 1){
//print the big image
echo $value["id"];
}else{
//print the little image
echo $value["id"];
}
$n++;
}
}else{
echo "No data founded";
}

How I can display all rows in php

I wrote this code to retrieve some rows form database
session_start();
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = " . $_SESSION['user_id'] ." and password = " . $_SESSION['user_pass'];
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result))
{
echo "this academic transcripts for " . $row["name"];
echo " and the id is " . $row["id"];
}
$q1 = " SELECT student_record.course,student_record.grade,student_record.term,coe_courses.crd
FROM student_record INNER JOIN coe_courses ON student_record.course_number = coe_courses.course_number
where student_record.id = ".$_SESSION['user_id'] ;
$result = mysqli_query($con , $q1 ) ;
if($row = mysqli_fetch_array($result))
{
echo "<br />";
echo "<table border=\"1\" style=\"width:500\">";
echo "<tr>";
echo "<th>coe_courses</th>";
echo "<th>terms</th>";
echo "<th>Grades</th>";
echo "<th>CRD</th>";
echo "</tr>";
echo "<tr>";
echo "<td>" . $row["course"]. "</td>";
echo "<td>" . $row["term"]. "</td>";
echo "<td>" . $row["grade"]. "</td>";
echo "<td>" . $row["crd"]. "</td>";
echo "</tr>";
echo "</table>";
}
The problem is that only shows the first row while I have three rows in phpMyAdmin.
enter image description here
You need to call fetch_* repeatedly to retrieve all rows from your result set; each time you call it it retrieves the next row in the result set.
In your sample code above, you would replace
if ($row = mysqli_fetch_array($result))
{
with
while ($row = mysqli_fetch_array($result))
{
This will loop until fetch_array tries to read beyond the last record in $result, at which point fetch_array returns false and the loop exits.

PHP Show results else nothing to display

I have this script here that shows a list of results. How to do l say "No results" if no results are found. I believe it's the else statement but couldn't quiet get it to work.
<?php
$result = mysql_query("SELECT * FROM emailquotes order by id desc")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo "<tr height='25px' valign='center'>";
echo '<td valign="middle"><p><img src="../../Images/Icons/table-delete.png"/></p></td>';
echo '<td><p>' . $row['ssp'] . '</p></td>';
echo '<td><p>' . $row['ssp'] . '#someonewhere.com</p></td>';
echo '<td><p>' . $row['surname'] . '</p></td>';
echo '<td><p>Car</p></td>';
echo '<td><p>Show Prices</p></td>';
echo "</tr>";
}
?>
You can try with mysql_num_rows function:
$count = mysql_num_rows($result);
if ($count > 0) {
// loop rows
} else {
// no result
}
Put your while loop in a if-statement and check if there is any results before running the loop. Then you echo "No results" in the else.
It should be like this:
<?php
$result = mysql_query("SELECT * FROM emailquotes order by id desc")
or die(mysql_error());
if( mysql_num_rows($result)) {
while($row = mysql_fetch_array( $result )) {
echo "<tr height='25px' valign='center'>";
echo '<td valign="middle"><p><a href="delete.php?id=' . $row['id'] . '"><img
src="../../Images/Icons/table-delete.png"/></a></p></td>';
echo '<td><p>' . $row['ssp'] . '</p></td>';
echo '<td><p>' . $row['ssp'] . '#someonewhere.com</p></td>';
echo '<td><p>' . $row['surname'] . '</p></td>';
echo '<td><p>Car</p></td>';
echo '<td><p>Show Prices</p></td>';
echo "</tr>";
}
}
else {
echo "No Result";
}
?>

how to echo sum value in row

i want to echo sum of 1st script in to 2nd script
and both are same page i was try but not working
example
echo '<td>' . $row['status'] . '</td>';
echo '<td>' .$final_result4. '</td>';
how can i do this please help me to fix this issue thanks....
this is my 1st script
$res1 = (($basicsalary+$allowsalary)*$days*$yeardays1)/$yeardays/$monthdays;
$res2 = (($basicsalary1+$allowsalary1)*$days*$yeardays2)/$yeardays/$monthdays;
$res3 = (($basicsalary2+$allowsalary2)*$days*$yeardays3)/$yeardays/$monthdays;
$res4 = (($basicsalary+$allowsalary)*$days*$yeardays4)/$yeardays/$monthdays;
$res5 = (($basicsalary1+$allowsalary1)*$days*$yeardays5)/$yeardays/$monthdays;
$res6 = (($basicsalary2+$allowsalary2)*$days*$yeardays6)/$yeardays/$monthdays;
$res8 = 221/730*$miscamount +$otheramount;
$res7 = $startdays -$enddays;
$result1 = number_format((round($res1, 1)),3);
$result2 = number_format((round($res2, 1)),3);
$result3 = number_format((round($res3, 1)),3);
$result4 = number_format((round($res4, 1)),3);
$result5 = number_format((round($res5, 1)),3);
$result6 = number_format((round($res6, 1)),3);
$result7 = number_format((round($res7, 1)),3);
$result8 = number_format((round($res8, 1)),3);
$final_result = number_format(($result1 +$result2 +$result3 +$result4 +$result5 +$result6 ),3);
$final_result2 = number_format((round($final_result/730*$result7 ,1 )),3);
$final_result3 = number_format((round(($final_result2 +$result8) , 1)),3);
echo $final_result4 = number_format((round(($final_result -$final_result3) , 1)),3);
//Ending of php
?>
this is my 2nd script
echo "<span align='center' class='style2'>Over All Report For The Period Of $a to $b</span>";
echo "<div id='testTable' id='non-printable'><table class='hovertable' border='1' cellpadding='10'>";
echo "<tr> <th>Date</th><th>Id</th><th>Name</th> <th>Division</th><th>Payment</th><th>Status</th><th>Total</th></tr>";
// get results1 from database
$result1 = mysql_query("SELECT * FROM fullfsaccounts where date BETWEEN '$a' AND '$b' order by date ASC");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['cardno'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['division'] . '</td>';
echo '<td>' . $row['typecash'] . '</td>';
echo '<td>' . $row['status'] . '</td>';
echo '<td>' '</td>';
echo "</tr>";
//Increment the value of the Total_total variable
//by the salary value of one row till the while loop finishes
$Total_rows=$num=mysql_num_rows($result1);
$Total_total=$Total_total+$row['total'];
}
echo "</table>";
echo "<table class='hovertable' border='1' >";
echo "<tr width='100%'>";
echo '<td>Total</td>';
echo '<td>' . $Total_rows .'</td>';
echo '<td>' . $Total_total .'</td>';
echo "</tr>";
// close table>
echo "</table>";
in your code you have one basic error in line echo '' ''; your need
put echo '' . '';
I think you also do not explain well because if they are on the same page as you say you should not have any problems to call the value of the local variables of the page.
I advise you to put different -> echo 'pass'; or echo 'pass : '.resultxxx: in controls the flow of the page, I understand that if they are on the same page and do not get out and walk in it you should be able to pick up the value of the local variables without any problem.
Please explain more your problem if you need help.

Require help validating data inside MySQL

I currently have 2 different sections for this program, the first half takes the users input from a web page and then transfers it over onto a PHP side which will access MySQL and display the requested information.
Example: If I enter AX12 for the ID it will display information for that ID which does infact exist, but if I enter AX13 (which doesn't) it will display blank information, so I'm wondering if someone can show me how I can validate this once the information has been transferred over onto the PHP side. So if it detects that the information you've submitted does not exist simply display a message saying "ID DOES NOT EXIST" or something along those lines.
Here's the code for the PHP side if you need it for more information.
<?php
$part_number = $_GET['txtInput'];
$part_description;
$units_on_hand;
$item_class;
$warehouse_number;
$unit_price;
$query;
$result_set;
$connection;
$record;
echo "<html>";
echo "<head>";
echo "<title>SQL Application</title>";
echo "<style type = 'text/css'>body{text-align: center; background-color: #CC3333; color: #660000; font-size: 30;}</style>";
echo "</head>";
echo "<body>";
echo "<center><h1>SQL Application</h1></center>";
echo "<br />";
echo "<br />";
echo "<br />";
$connection = #mysql_connect("localhost","m_stanicic","")
or die ("\n\n PROBLEM CONNECTING TO DATABASE! \n" . mysql_error() . "\n\n");
mysql_select_db("m_stanicicdb");
$query = "select * from part where part_number = '" . $part_number . "'";
$result_set = mysql_query($query)
or die ("\n\n PROBLEM WITH QUERY! . \n" . mysql_error() . "\n\n");
$record = mysql_fetch_assoc($result_set);
if($part_number == "")
{
//
}
else
{
$part_description = $record['part_description'];
$units_on_hand = $record['units_on_hand'];
$item_class = $record['item_class'];
$warehouse_number = $record['warehouse_number'];
$unit_price = $record['unit_price'];
echo "<center>";
echo "<table border='1' width=400 style ='table-layout:fixed' cellpadding='5' cellspacing='0'>";
echo "<col width = 200>";
echo "<col width = 200>";
echo "<tr>";
echo "<th colspan='2'>DETAILS OF THE PART YOU REQUESTED</th>";
echo "</tr>";
echo "<tr>";
echo "<td>part_description</td>";
echo "<td>" . $part_description . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>units_on_hand</td>";
echo "<td>" . $units_on_hand . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>item_class</td>";
echo "<td>" . $item_class . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>warehouse_number</td>";
echo "<td>" . $warehouse_number . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>unit_price</td>";
echo "<td>$" . $unit_price . "</td>";
echo "</tr>";
echo "</table>";
echo "</center>";
mysql_close($connection);
}
echo "<br />";
echo "<br />";
echo "<br />";
echo "<input type = 'button' value = 'RETURN' style = 'width: 75px; height: 75px;' onclick = \"javascript:window.location.href = 'jdpset1_4.html'\">";
echo "</body>";
echo "</html>";
You aren't validating anywhere that the result did return any data at all. Right after your call to mysql_query(), you should use mysql_num_rows() to see how many rows were returned by your query -- if mysql_num_rows($result_set) is zero, your query returned no data.
Notice how $part_number is never modified by mysql_query(), mysql_fetch_array() or any of those functions; so it will never be empty unless it started as such (rendering your current if almost useless).
You can check the output of your query $record...
if (count($record)==0) {
echo "the ID you entered does not exist! Try again...";
} else {
// code to output the part's details...
}
put the if (count... part instead of ...
if($part_number == "")
from your code i notice 2 things
$query = "select * from part where part_number = '" . $part_number . "'";
as your part number is a string, i recommend you to use LIKE not =
$query = "select * from part where part_number LIKE '" . $part_number . "'";
another is inspect your record is returning in multidimensional array like
$record = Array([0]=>array('part_description'=>A123...)).
then you must assign like so
$part_description = $record[0]['part_description'];
i hope it helps you

Categories