PHP mysql display query in 4 columns - php

How to display this query result in 4 columns
<?php
$direction = $_POST['direction'];
$sumword = $_POST['sumword'];
$length = $_POST['length'];
$con = mysql_connect("localhost","elsha","12q(5PSZ.");
$db = mysql_select_db("elsha",$con);
$query = "SELECT answer FROM words WHERE direction = '$direction' AND sumword = '$sumword' AND sumletter = '$length'";
$result = mysql_query($query);
if(mysql_error()) {
//check that no error has occurred first; take this out in production or make more graceful handling
die(mysql_error());
}
if(mysql_num_rows($result) == 0) {
echo "No Results";
} else {
while($row = mysql_fetch_array($result)) {
echo '<img src="/images/'. $row['0'].'.jpg"><br> '. $row['0'].'<br>';
}
}
?>
like this :
result1 result2 result3 result4
result5 result6 result7 result8

Try this
$i = 0;
while($row = mysql_fetch_array($result)) {
echo '<img src="/images/'. $row['0'].'.jpg"> ';
$i++;
if($i % 4 == 1 && $i!=1){
echo '<br>';
}
}

Use flag like
$flag=0;
while($row = mysql_fetch_array($result)) {
if(($flag%4)==0) {
echo '<tr>';
}
echo '<td><img src="/images/'. $row['0'].'.jpg"><br> '. $row['0'].'</td>';
if(($flag%4)==3) {
echo '</tr>';
$flag=-1;
}
$flag++;
}

replace your while with the following code. try this
$a=1;
while($row = mysql_fetch_array($result)) {
if($a%4==0) {
echo '<img src="/images/'. $row['0'].'.jpg">'. $row['0'].'<br>';
} //4th line with break
else {
echo '<img src="/images/'. $row['0'].'.jpg"> '. $row['0'].' '; // prints first 3 lines
}
$a=$a+1;
}

I think you are trying to display the image name too below the image. Try this:
$i = 1;
while($row = mysql_fetch_array($result)) {
echo '<div class="imgs">';
echo '<img src="/images/'. $row['0'].'.jpg"><br> '. $row['0'];
echo '</div>';
if ($i === 4) {
echo '<div class="clear"></div>';
$i = 1;
} else {
$i++;
}
}
and then style div's
.imgs {
float: left;
/* other styles */
}
.clear {
clear: both;
}

Related

Make new table after reaching a certain row count

I created a code that converts characters to binary and make table cells black/white corresponding to the ones and zeros. This is my code:
$str_splt = str_split($text);
echo "<table>";
for ($a=0;$a < count($str_splt);$a++) {
$bits = array(128,64,32,16,8,4,2,1);
$store = array(0,0,0,0,0,0,0,0);
$inp = ord($str_splt[$a]);
for ($x=0;$x < count($bits);$x++) {
if ($bits[$x] <= $inp) {
$inp = $inp - $bits[$x];
$store[$x] = 1;
} else {
$store[$x] = 0;
}
};
$store_rvs = array_reverse($store);
echo "<tr>";
for ($b=0;$b < count($store_rvs);$b++) {
if ($store_rvs[$b] == '1') {
echo "<td id=\"blk\"></td>";
}
else {
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</table>";
Its output looks like this ($text = "ABCDEFGH"):
As you can see it's 8x8 table. I want to add the next set of bytes to the side of that table like this:
Each 8x8 table is a group. The two images above is group 1 and group 2:
I want to display the tables like this but I can't find the solution.
I did it in this way. Ignore my css if you are fine with yours. I replaced the id tag with class because each id should be defined once only.
echo "<html><head>";
echo "<style type='text/css'>";
echo " table, td { padding:0px; margin:0px; }";
echo " td.cell { width:15px; height:15px; }";
echo " td.blk { background-color:black; }";
echo " td.wht { background-color:yellow; }";
echo "</style>";
echo "</head><body>";
$text = "ABCDEFGH";
$text.= "ABCDEFGH";
echo "<table><tr><td><table>";
for($a=0; $a<strlen($text); $a++) {
$chr = substr($text,$a,1);
$bits = array(128,64,32,16,8,4,2,1);
$store = array(0,0,0,0,0,0,0,0);
$inp = ord($chr);
for($x=0; $x<count($bits); $x++) {
if($bits[$x] <= $inp) {
$inp = $inp - $bits[$x];
$store[$x] = 1;
} else {
$store[$x] = 0;
}
}
$store_rvs = array_reverse($store);
if($a % 8 === 0) {
echo "</table></td><td><table>";
}
echo "<tr>";
for($b=0; $b<count($store_rvs); $b++) {
if($store_rvs[$b] == '1') {
echo "<td class='cell blk'></td>";
} else {
echo "<td class='cell wht'></td>";
}
}
echo "</tr>";
}
echo "</table></td></tr></table>";

mysqli/PHP - first two rows never show up(table with row data going across columns then moving down)

My website currently ignores the first two images you place into the database and then proceeds to add images going across 5 columns and then moving down to the next row.
Update: Now it shows 3 of the 4 images in the database. Skips one image.
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
This is what my database looks like
http://i.stack.imgur.com/IFba8.jpg
This is what my website shows
http://i.stack.imgur.com/Wf7E1.jpg
Try this:
<?php
$i = 1;
echo "<table>";
while ($row = $Recordset2->fetch_object()) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>
Please try this.
<?php
$i = 1;
echo "<table>";
while ( $row = $Recordset2->fetch_object() ) {
if ($i == 1) {
echo "<tr>";
}
echo '<td><img src="'.$row_Recordset2['ImgSource'].'" width="100" height="100"></td>';
if ($i == 5) {
$i = 1;
echo "</tr>";
} else {
$i++;
}
}
echo "</table>";
?>

PHP Ranking function with TIES

So I have a function that takes a number and outputs it as a placement. How do I go about making the function take in consideration ties. I have a ranking database and this php code echos out rankings on a table. right now it ranks but it doesn't consider ties. How do i go about doing this
<?php
function addOrdinalNumberSuffix($num) {
if (!in_array(($num % 100),array(11,12,13))){
switch ($num % 10) {
// Handle 1st, 2nd, 3rd
case 1: return $num.'st';
case 2: return $num.'nd';
case 3: return $num.'rd';
}
}
return $num.'th';
}
?>
<?php
$link = mysqli_connect("localhost", "citricide", "321213123Lol", "juneausmashbros");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT * FROM rankings ORDER BY points DESC";
$result = mysqli_query($link, $query);
echo '<article class="content grid_6 push_3">';
echo '<h1>';
echo 'Project M Summer Ranbat Rankings';
echo '</h1>';
echo '<section>';
echo '<center>';
echo '<table style="width:400px" class="rankslist">';
echo '<tr>';
echo '<th width="15%"><b>Rank</b></th>';
echo '<th width="45%"><b>Name</b></th>';
echo '<th width="45%"><b>Alias</b></th>';
echo '<th width="15%"><b>Points</b></th>';
echo '</tr>';
$ass = 0;
while($row = $result->fetch_array()) {
$ass++;
echo '<tr>';
if ($ass == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($ass == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($ass == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($ass);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
echo '</table>';
echo '</center>';
echo '</section>';
echo '</article>';
?>
It seems like you need additional variables to track the rank and the previous value. By doing so, you can handle ties.
For example:
$ass = 0; // current record count
$rank = 0; // rank
$last_points = NULL; // variable to store last ranked value
while($row = $result->fetch_array()) {
$ass++;
// check if value changes and reset rank if it does
if ($row['points'] !== $last_points) {
$rank = $ass;
$last_points = $row['points'];
}
echo '<tr>';
if ($rank == 1) {
echo ' <center><td><B><font color=#FFD700>';
} else if ($rank == 2) {
echo ' <center><td><B><font color=#CCCCCC>';
} else if ($rank == 3) {
echo ' <center><td><B><font color=#cd7f32>';
} else {
echo '<td>';
}
echo addOrdinalNumberSuffix($rank);
echo ' </font></B</td></center>';
echo ' <td>'.$row['name'].'</td>';
echo '<td>'.$row['alias'].'</td>' ;
echo '<td>'.$row['points'].'</td>';
echo '</tr>';
}
So say your points looked like this:
100
100
90
80
The $rank value would be:
1
1
3
4

Images from database column not loading

CODE:
<?php
$page = $_GET['page'];
$category = $_GET['cat'];
$your_db = # new mysqli("database","name","password");
if (mysqli_connect_errno())
{
echo 'ERROR!
'.mysqli_connect_errno()
.' - Not connected : '.mysqli_connect_error().'
';
die;
}
else
{
$db_connect = $your_db->select_db("databasename");
if (!$db_connect)
{
echo 'ERROR CONNECT DATA BASE';
die;
}
}
echo '<p>';
$query = "select distinct fldCity from info order by fldCity";
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
for ($i = 0; $i < $number_of_records; $i++)
{
$row = $result->fetch_assoc();
echo '<a href="index.php?cat='.stripslashes($row['fldCity']).'">';
echo stripslashes($row['fldCity']);
echo '</a> / ';
}
echo '</p>';
if ($category)
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where fldCity = '$category' and length(fldCompanyLogo) > 0 order by fldCity";
}
else
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where length(fldCompanyLogo) > 0 order by fldCity";
}
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
$num_pages = $number_of_records / 7;
if (($number_of_records % 7) > 0 )
{
$num_pages++;
}
if (strlen($page) == 0)
{
$page = 0;
}
else
{
$page = $page * 4;
}
echo '<table border="0" cellspacing="10" cellpadding="10" style="border-collapse: collapse" bordercolor="#111111">';
$row_num = 4;
$result->data_seek($page);
for ($i = $page; $i < $number_of_records; $i++)
{
if ($row_num <= 4)
{
echo '<tr>';
for ($col_num = 0; $col_num < 4; $col_num++)
{
$row = $result->fetch_assoc();
echo '<td>';
show_image(stripslashes($row['fldCompanyLogo']),stripslashes($row['fldCompanyName']));
echo '
';
echo '<b><font face="Tahoma" size="2"><a href="mysite.ca/'.stripslashes($row['fldMenuLink']).'" target="_blank"></font>';
echo '<font face="Tahoma" size="2"><a href="'.stripslashes($row['fldMenuLink']).'" target="_blank">';
echo stripslashes($row['fldCompanyName']);
echo '</a>';
echo '
';
echo stripslashes($row['fldCity']);
echo '
';
echo stripslashes($row['fldProvince']);
echo '
';
echo stripslashes($row['fldPostalCode']);
echo '</td>';
}
$row_num++;
echo '</tr>';
}
else
{
break;
}
}
echo '</table>';
for ($j = 0; $j < $num_pages; $j++)
{
$page_link = $j + 1;
echo '<font face="Tahoma" size="4"><b>'.$page_link.'</b></font> ';
}
echo ' '.$number_of_records;
function show_image($image_name)
{
if (file_exists("'$image_name'"))
{
$dim_img = getimagesize('/images/'.$image_name);
echo '<img src="/images/'.$image_name.'" alt = '.$alt.' border=0 align="bottom"';
echo 'width = '. $dim_img[100] .' height = ' .$dim_img[100] . ' />';
}
else
echo 'Add your image here!';
}
?>
I am totally lost and I can't find the error to why the images from the database isn't loading with the script. I highlighted the area in blue where I believe the error is to be. I just can't find any errors in that particular spot! All I want is the images from a column I have in my database to be fetched and connected to 'echo '
No images are being displayed, and I can't find the error as to why the images aren't showing up
try Convert Image to Base64 String and Base64 String to Image
also why echo ''; you use this its makes no sense

php mysql_fetch_array every 25 items wrap a div

I want to mysql_fetch_array 100 items from mysql database. then every 25 items wrap a div.
also, I make a total items result check.
My code as below, but it will add <div> to every item, no as my require. So how to make it easier? Thanks.
if (mysql_num_rows($result) != 0) {
$total = mysql_num_rows($result);
$num = 1;
while ($row = mysql_fetch_array($result)) {
if ($num === 1) {
echo '<div>';
}
echo $row['title'] . '<br />';
if ($total < 26) {
echo '</div>';
}
else {
if ($num === 26) {
echo '<div>';
}
if ($total < 51) {
echo '</div>';
}
else {
if ($num === 51) {
echo '<div>';
}
if ($total < 76) {
echo '</div>';
}
else {
if ($num === 75) {
echo '<div>';
}
if ($total < 101) {
echo '</div>';
}
}
}
}
$num++;
}
}
You can use the mod operator.
See: http://php.net/manual/en/internals2.opcodes.mod.php
echo '<div>';
while($row = mysql_fetch_array($result)){
....
if (($num % 25) === 1) { echo '</div><div>' }
$num++;
}
echo '</div>';
Try This...
if(mysql_num_rows($result)!=0){
$total = mysql_num_rows($result);
$num=1;
while($row = mysql_fetch_array($result)){
if($num===1)
echo '<div>';
echo $row['title'].'<br />';
if($num==25){
echo '</div>';
$num=1;
}
$num++;
}
}
use the mod operator.
$num =1;
while($row = mysql_fetch_array($result)){
if (($num % 25) === 1) { echo '<div>' }
-------here data ----
if (($num % 25) === 0) { echo '</div>' }
$num++;
}
My advice is to separate your concerns... Give it a shot more like this:
// first get all your data.
$results = array();
while($row = mysql_fetch_array($result)){
$results[] = $row;
}
// now you have an array of all of your records.
if (!empty($results)) {
// total count of the array up front.
$total = count($results);
$interval = 0;
// save the data in a buffer for echoing later.
$buffer = array('<div>');
foreach($results as $row) {
$buffer[] = $row['title'] . '<br />';
if (++$interval === 24) { // notice the prefix ++
$interval = 0;
// close and re-open divs
$buffer[] = '</div><div>';
}
}
// one final close tag
$buffer[] = '</div>';
// now we zip it up and echo the content.
echo implode('', $buffer);
}
Here is how I do it...
$count = 0;
while($row = mysql_fetch_array($result)){
if ($count%$25 == 0)
{
echo "<div>";
}
//whatever your data goes here
count++
if ($count%$25 == 0)
{
echo "</div>";
}
}

Categories