how to have two images in one row in php from array - php

Please help me I have just pulled images from gallery using PHP. But now I just want to display two images in one row. Return it in $content that I have specified.
Please help me.
function CreateGalleryImages($types) {
$galleryArray = array();
$galleryModel = new GalleryModel();
$galleryArray = ($galleryModel -> getGalleryByTypes($types));
$result = "";
foreach ($galleryArray as $gallery) {
for ($i = 0; $i < 2; $i++){
$result = "<td>"
."<img runat = 'server' src = 'http://localhost/schoolwb/event/$gallery' height=500 width=500 />"
."</td>";
}
$result = "<tr>.$result.</tr>";
}
return "<table class = 'GalleryTable'> . $result . </table>";
}

You have to concatenate the result and to store in $result.
Try This
<?php
function CreateGalleryImages($types){
$galleryArray = array();
$galleryModel = new GalleryModel();
$galleryArray = ($galleryModel->getGalleryByTypes($types));
$result = "";
foreach ($galleryArray as $gallery){
$cl = "";
for ($i = 0;$i<2;$i++){
$cl .= "<td>"
."<img runat = 'server' src = 'http://localhost/schoolwb/event/$gallery' height=500 width=500 />"
."</td>";
}
$result .= "<tr>.$cl.</tr>";
}
return "<table class = 'GalleryTable'>.$result.</table>";
}
?>

Related

PHP loop outputting duplicates

public function select(){
$rows = [];
$connection = $this->connect();
$result = $connection->query("SELECT username FROM users");
while ($row = $result->fetch_assoc()){
$rows[] = $row;
}
$userlist = 0;
foreach($rows as $username){
$userlist .= $username['username'];
}
$get_rankings = [1,2,3,4];
$get_image_path = "images/";
$total = 0;
for ($x = 0; $x < count($get_rankings); $x++){
$total = $get_rankings[$x];
$path .= "<img src = '" . $get_image_path . $total . ".png'>\n" . $userlist . "<br/>";
// echo "<span class = 'align-down'>{$path}";
// echo "<p class = 'user-name'> {$rows['0']}</p>";
// echo "</span>";
}
echo $path;
}
I'm trying to output a simple ranking but using the number index as images to display them.
In the past i've tried to do something similar but couldn't figure out how to match player it with images on the side.
The output im getting is this:
It's outputting each entry 4 times(I get why, its in a loop) but I can't figure out the correct solution to write it outside of a loop or properly
The desired output is:
My DataBase reads as:
[id][username][password]
If there is an easier solution, i'm all ears. I don't know how to approach this.
There's no need for $userlist. Output the username from $rows[$x].
$path = "";
$max = min(count($rows), count($get_rankings));
for ($x = 0; $x < $max; $x++){
$total = $get_rankings[$x];
$path .= "<img src = '" . $get_image_path . $total . ".png'>\n" . $rows[$x]['username'] . "<br/>";
// echo "<span class = 'align-down'>{$path}";
// echo "<p class = 'user-name'> {$rows['0']}</p>";
// echo "</span>";
}

Display images from a database into a PHP page

Im wondering what can I do to make images from a database on Php display on my page.
This is what I have
images .php
$query = "SELECT * FROM images ORDER BY name ASC ";
$result = $db->query($query);
$num_result = $result->num_rows;
echo "<h1> Images</h1>";
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array = array($URL);
}
foreach ($array as $image){
echo '<tr>';
echo '<td><img class="coupons" src="'.$image.'"/></td>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
}
This is just printing only one image and I have 10 in my database, what can I do or change to print all of the images from the database? Thanks
You should change
$array = array($URL);
into
$array[] = $URL;
And add before line:
for ($i = 0; $i < $num_result; $i++){
add line:
$array = array();
Try this,
$array = array();//initialize here
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array[] = $URL;
}
You can rewrite your code as,
while ($row = $result->fetch_assoc()){
$name = $row['name'];
$URL = $row['imageURL'];
echo '<tr>';
echo '<td><img class="coupons" src="'.$URL.'"/></td>';
echo '<td></td>';
echo '</tr>';
echo '<tr>';
}
$query = "SELECT * FROM images ORDER BY name ASC ";
$result = $db->query($query);
$num_result = $result->num_rows;
$array = array();
echo "<h1> Images</h1>";
for ($i = 0; $i < $num_result; $i++){
$row = $result->fetch_assoc();
$name = $row['name'];
$URL = $row['imageURL'];
$array[] = URL;
}
foreach ($array as $image){
echo '<tr>';
echo '<td><img class="coupons" src="'.$image.'"/></td>';
echo '<td></td>';
echo '</tr>';
}
You also had a trailing <tr> which may cause styling issues

Echoing out a variable variables list

I need to create a string like ‘Acme’, ‘Umbrella’, ‘Waymart’ for use in a java function. So far, I have:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0=odbc_exec($conn1,$info0);
$count = 1;
while($row = odbc_fetch_array($rs0))
{
${'V'.$count++} = "" . $row['Company'] . "";
}
$categories = "'$V1', '$V2', '$V3'";
echo $categories;
I then have $categories echo in the place the function needs the company list. This work as long as there are just 3 companies. But when more are added, I’ll have to add ‘$V4’, ‘$V5’ and so on. Any ideas? Thanks.
Use arrays instead of variable variables, you can then implode the results:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0 = odbc_exec($conn1,$info0);
$companies = array();
while($row = odbc_fetch_array($rs0)) {
$companies[] = "'" . $row['Company'] . "'";
}
$categories = implode(', ', $companies);
echo $categories;
... if you would like to continue with variable variables, use a loop to create your string:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0=odbc_exec($conn1,$info0);
$count = 1;
while($row = odbc_fetch_array($rs0)) {
${'V'.$count++} = "" . $row['Company'] . "";
}
$categories = '';
for($x = 1; $x <= $count; $x++) {
if($x > 1) $categories .= ', '; // add the comma
$categories .= "'" . ${'V'.$x} . "'"; // add the variable variable
}
echo $categories;
Please try this:
$info0 = "SELECT DISTINCT Company FROM CETracker";
$rs0=odbc_exec($conn1,$info0);
$count = 1;
$str = '';
while($row = odbc_fetch_array($rs0))
{
$str .= "'".$row['Company']."',";
}
$str = substr($str,0,strlen($str)-1);
echo $str;

Using DOM to get dynamic table data but not following tables sort

I've got a table that is default sorted ascendingly by its 2nd column by numeric value.
I'm trying to grab the top 5 rows from this table, but my script doesn't seem to want to follow the tablesorter forced sort order I have, it's just taking the default non sorted data.
So how do I get my script to grab the sorted table data? I don't want to have to grab all the data and sort it again for this script.
here is my code
<?php
include("bestbrokers_array.php");
require('simple_html_dom.php');
$table = array();
$html = file_get_html('http://www.forexfbi.com/best-forex-brokers-comparison/');
$num=1;
foreach($html->find('tr') as $row) {
if($num <= 6)
{
$rating = $row->find('td',1)->innertext;
$name = $row->find('td',0)->plaintext;
$table[$name][$rating] = true;
$num++;
}
}
html_show_array($table);
?>
and..
<?php
function do_offset($level){
$offset = ""; // offset for subarry
for ($i=1; $i<$level;$i++){
$offset = $offset . "<td></td>";
}
return $offset;
}
function show_array($array, $level, $sub){
if (is_array($array) == 1){ // check if input is an array
foreach($array as $key_val => $value) {
$offset = "";
if (is_array($value) == 1){ // array is multidimensional
echo "<tr>";
$offset = do_offset($level);
echo $offset . "<td>" . $key_val . "</td>";
show_array($value, $level+1, 1);
}
else{ // (sub)array is not multidim
if ($sub != 1){ // first entry for subarray
echo "<tr nosub>";
$offset = do_offset($level);
}
$sub = 0;
echo $offset . "<td main ".$sub." width=\"120\">" . $key_val .
"</td>";
echo "</tr>\n";
}
} //foreach $array
}
else{ // argument $array is not an array
return;
}
}
function html_show_array($array){
echo "<table cellspacing=\"0\" border=\"2\">\n";
show_array($array, 1, 0);
echo "</table>\n";
}
?>

php report is very slow and crashes in firefox

I have a report that runs and returns 366 records, each containing a thumbnail that is 104 x 80 px. The issue is that the report runs very slowley even though I increased the memory size.
ini_set('memory_limit', '128M');
ini_set('max_execution_time','600');
After writing the SQL query I generate the table items here
generate_table_items($query_all_items);
This then runs through and checks for the image in the columns
function generate_table_items($query){
$columns = array();
$resultset = array();
$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$columncount = 0;
$rowcost = 0;
$rowsale = 0;
while ($row = mssql_fetch_assoc($query)) {
if (empty($columns)) {
$columns = array_keys($row);
echo '<tr><th scope="col" >'.implode('</th><th scope="col" >',get_column_name($columns)).'</th></tr>';
$columncount = sizeof(array_keys($row));
}
$resultset[] = $row;
echo '<tr><td>'.implode('</td><td>',report_image_check($row)).'</td></tr>';
if(in_array($_SERVER['SCRIPT_NAME'],$scriptname)){
$colspan = (count($columns)-2);
echo "<tr><th scope='row'>Documents</th><td colspan='$colspan' >";
$PKID = $row['ID'];
if($row['SumOfTotalCost'] || $row['SumOfSalePrice']){
$rowcost += $row['SumOfTotalCost'];
$rowsale += $row['SumOfSalePrice'];
$get_total = true;
}
$query_docs = mssql_query("select documents.* from dbo.documents where documents.Antiquities_id = $PKID") or die ('get docs query failed ' . mssql_get_last_message());
while($row_docs = mssql_fetch_assoc($query_docs)){
$document = "../documents/" . $row_docs['document'];
echo "<a href='$document' title='opens in a new window' target='_blank' >" . $row_docs['document'] . "</a> | ";
} // End while (list docs)
mssql_free_result($query_docs);
echo "</td></tr>";
myflush();
} // End if all items and all items by value report
} // End While
echo '<tr>';
for($i=0;$i < $columncount-4;$i++){
echo '<td> </td>';
}
echo '<td>Total Cost '. $rowcost.'</td>';
echo '<td>Total Sale '. $rowsale.'</td>';
echo '<td>Total Calculated Difference '. ($rowsale-$rowcost).'</td></tr>';
} // End function
function get_column_name($columns){
$newcol = array();
$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$thecount = 0;
foreach($columns as $col) {
if($thecount == 1 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
// Don't list the PK
} else {
$newcol[] = '<img id="'.$col.'" src="../images/icons/arrow_down.png" alt="click to sort by" onclick="sortby(\''.$col.'\');" />' . $col;
}
$thecount++;
}
/*if(in_array($_SERVER['SCRIPT_NAME'],$scriptname)){
$newcol[] = "documents";
}*/
return $newcol;
}
function report_image_check($row){
global $base_url, $uploaded_images_folder;
$newrow = array();
$imageext = array();
$imageext[0] = ".jpg";
$imageext[1] = ".png";
$imageext[2] = ".gif";
$imageext[3] = ".tiff";
$scriptname = array();
$scriptname[0] = "/reports/all_items.php";
$scriptname[1] = "/reports/all_items_by_value.php";
$PKID = 0;
$thecount = 0;
foreach($row as $rn) {
if(in_array(strtolower(substr($rn,-4)),$imageext)){
$small_img_ext = substr($rn,-4);
$small_img = substr($rn,0,strripos($rn,"."));
$small_img = $small_img . '_140_105' . $small_img_ext;
$newrow[] = '<a href="' . $base_url . $uploaded_images_folder . '/' . $small_img . '" title="click to zoom on image" target="_blank" ><img src="' . $base_url . $uploaded_images_folder . '/' . $rn . '" alt="" width="50px" height="50px" /></a>';
} elseif($thecount == 1 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
$PKID = $rn;
} elseif($thecount == 2 && in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
$newrow[] = "<a href='../index.php?template=10&PKID=$PKID' target='_blank' >$PKID (click to view)</a>";
} else {
$newrow[] = $rn;
}
$thecount++;
myflush();
}
/*if (in_array($_SERVER['SCRIPT_NAME'],$scriptname)) {
$newrow[] = "<a href='#&PKID=$PKID' target='_blank' >Documents (click to view)</a>";
}*/
return $newrow;
} // End function
//// Flushing function
function myflush(){
ob_implicit_flush();
ignore_user_abort();
}
Can anyone see an issue with this code or see why it would take so long or why it crashes firefox? Would printing to pdf function work better?
It'll take a long time because you're nesting SQL queries... executing a second SQL query for every result that has been returned by the first query.... Doing a single query with a JOIN should help performance significantly.
Printing to PDF would almost certainly be slower: you'd eithe rneed a lot of code to position everything correctly in the report yourself, or to use one of the libraries that can take HTML and render it to a PDF (as you're already generating HTML anyway at the moment, this would be additional processing)

Categories