cant display two image links from mysql php - php

function find_image_by_id() {
global $connection;
$query = "SELECT * ";
$query .= "FROM images ";
$query .= "WHERE page_id={$_GET["page"]}";
$image_set = mysqli_query($connection, $query);
confirm_query($image_set);
return $image_set;
}
function display_image_by_id(){
$current_image = find_image_by_id();
while($image=mysqli_fetch_assoc($current_image)){
$output = "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}
This is the code I'm using to show the images stored as links in mysql
and the images are in a folder. But what happens after this code is executed only the second
value is displayed. I want both value/ images to be displayed.

Try something like that-
All you need to do is just initialize this variable outside the loop.
$output =''; //initialize before
SO your function look like this -
function display_image_by_id(){
$current_image = find_image_by_id();
$output =''; //initialize before
while($image=mysqli_fetch_assoc($current_image)){
$output .= "<div class=\"images\">";
$output .= "<img src=\"images/";
$output .= $image["ilink"];
$output .= "\" width=\"72\" height=\"72\" />";
$output .= $image["phone_name"];
$output .= "</div><br />";
}
mysqli_free_result($current_image);
return $output;
}

Related

PHP While Loops Table Mysqli Results

I'm about month and a half into PHP. I tried to make function for fetching results from find_all_subjects():
function find_all_subjects() {
global $dbconnect;
$q = "SELECT * FROM subjects ORDER BY id ASC";
$subject_set = mysqli_query($dbconnect, $q);
confirm_query($subject_set);
return $subject_set;
}
and then making a new function which will loop the result in table rows. The problem is it loops just one result... when I did it with <ul> and <li> it worked fine but it doesn't seem to work when doing it with the table. My table core tags are in another document. So it's not that...
function navigacija() {
$s = find_all_subjects();
while($subjects = mysqli_fetch_assoc($s)) {
$out = "<tr>";
// Ime Teme
$out .= "<td width='25%' height='40'> <a href=\"admin_content.php?subject=" . urlencode($subjects['id']) . "\">";
$out .= $subjects['menu_name'];
$out .= "</a></td>";
// Vidljiva
if($subjects['visible'] == 1) {
$subjects['visible'] = 'DA';
} else {
$subjects['visible'] = 'NE';
}
$out .= "<td width='25%' height='40'><p align=\"center\">DA / NE";
$out .= "</p></td>";
// Broj Strana
$out .= "<td width='25%' height='40'>";
$pages_set = find_sub_from_pages($subjects['id']);
while($pages = mysqli_fetch_assoc($pages_set)) {
$out .= "" . $pages['menu_name'] . "";
$out .= "</td>";
}
// Vidljiva
$out .= "<td align=\"center\" width='25%' height='40'>";
$out .= "<img width=\"17px\" height=\"17px\" src=\"st/img/ic-arup.png\">
</img> <img width=\"17px\" height=\"17px\" src=\"st/img/ic-ardown.png\"></img> ";
$out .= "</td>";
$out .= "</tr>";
}
return $out;
}
the problem is that you reset
$out = "<tr>";
every time in the loop.
change this line to
$out .= "<tr>";
and only put declaration out of the loop
$out = "";

Reinitializing variable in php to NULL

I'm writing a simple gamePage. My Controller method looks like this:
$model = new Game143();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->verifyAnswers();
return $this->render('Game143-confirm', ['model' => $model]);
} else {
$model->initGame(1, 2);
return $this->render('Game143', ['model' => $model]);
}
The else is the important bit. The initGame method is passed the starting level and the max number of levels, so I'm iterating 2 times with random digits and everything works fine.
In my init method I set the value:
public function initGame($level, $numEx) {
$this->maxLevel = $numEx; // normally 10
$this->thisLevel = $level; //normally 1
I also did some output. This works fine and my variable is saved as 2.
In the if method of the controller my variable is suddenly set to NULL?!
Because of this even my results are NULL, because my iteration is <=maxLevel. Anyone know why this happens?
In verifyAnswers(); my given and initialized maxLevel is NULL.
Iterating not possible:
for(; $this->thisLevel <= $this->maxLevel; $this->thisLevel++) {
If i hardcode the maxLevel everything works fine also.
In the view (Working with yii-2):
for($model->thisLevel = 1; $model->thisLevel <= $model->maxLevel; $model->thisLevel++) {
$out .= "\n";
$out .= Html::activeHiddenInput($model,"digit1[$model->thisLevel]");
$out .= "\n";
$out .= Html::activeHiddenInput($model,"digit2[$model->thisLevel]");
$out .= "\n";
$out .= "\n";
$out .= "<h3>Level $model->thisLevel</h3>\n";
$out .= "<h3>max $model->maxLevel</h3>\n";
$out .= "\n";
$out .= "<div class=\"row aufgabenFeld\">";
$out .= "<div align=\"right\" class=\"col-lg-4 col-md-4 col-xs-4 wallLabel \">";
$out .= $model->digit1[$model->thisLevel];
$out .= "</div>";
$out.="<div id='myField__<?php echo $model->thisLevel; ?>' class=\"col-lg-2 col-md-2 col-xs-2 wall\">\n";
$out .= $form->field($model, "proposal[$model->thisLevel]")->label(false);
$out .= "</div>";
$out .= "<div align=\"left\" class=\"col-lg-4 col-md-4 col-xs-4 wallLabel \">"; //id test xD
$out .= $model->digit2[$model->thisLevel];
$out .= "</div>";
$out .= "</div>";
$out .= "<div><br></div>";
In the 'submitView':
foreach ($model->proposal as $key=>$value){
$out .= "<li><h4>Aufgabe $key</h4>";
$out .= $model->digit1[$key];
$out .= " ";
$out .= $model->proposal[$key];
$out .= " ";
$out .= $model->digit2[$key];
$out .= $model->maxLevel[$key];
$out .= "</li>";
}

TCPDF - Having trouble with getting MySQL content to show in PDF

I am trying to make a printable version of a catalogue of products. If I display the products in HTML it works fine. But when I try and use TCPDF I loose half of it or nothing at all.
$query = "SELECT ID, Category_Name, Image FROM Categories ORDER BY Position ASC";
$result = mysqli_query($connection, $query);
confirm_query($result);
while ($cat = mysqli_fetch_row($result)) {
// add a page
$pdf->AddPage();
$query1 = "SELECT * FROM Products WHERE Category = " . $cat[0] . " AND Visibility = 1 ORDER BY Product_Name ASC";
$result1 = mysqli_query($connection, $query1);
$html = $query1;
confirm_query($result1);
//$html .= '<style>'. file_get_contents('./images/print.css').'</style>';
$html .= '<table style="margin: 0 auto;"><tbody>';
$html .= '<tr><th colspan="6"><div id="title" style="color: white;">' . $cat[1] . '</div></th></tr>';
$html .= '<tr>';
$html .= '<th></th>';
$html .= '<th>Product</th>';
$html .= '<th>Pack Size</th>';
$html .= '<th>Price(Ex VAT)</th>';
$html .= '<th>Price(Inc)</th>';
$html .= '<th>RRP</th>';
$html .= '</tr>';
// Show product prodcuts from selected category
while ( $row123 = mysqli_fetch_row($result1)) {
$html .= '<tr><td>';
$html .= '<img width="100px" height="100px" src="/images/' . $row123[6] . '"/>';
$html .= '</td><td>';
$html .= '<b><u>' . $row123[1] . '</u></b><br /><i>' . $row123[5] . '</i>';
$html .= '</td><td>';
$html .= $row123[3];
$html .= '</td><td>';
$html .= '£' . $row123[4];
//echo '<br />';
$html .= '</td><td>';
$query2 = "SELECT VAT FROM VAT WHERE ID = " . $row123[7];
$result2 = mysqli_query($connection, $query2);
confirm_query($result2);
// Put results in an accessible form
$result_array2 = array();
while ($row234 = mysqli_fetch_row($result2)) {$result_array2[] = $row234;}
$html .= '£' . number_format($row123[4] + ($result_array2[0][0] * $row123[4] / 100), 2, '.', '');
$html .= '</td><td>';
if ($row123[9] == "") {
$html .= "N/A";
} else {
$html .= '£' . number_format($row123[9], 2, '.', '');
}
$html .= '</td>';
$html .='</tr>';
}
$html .= '</tbody></table>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
}
If I comment out the second while loop, I can get the first query ($query1) to echo out. One per page for each category. But when I uncomment it, it just creates 4 blank pages.

Query not returning in order

If you require any additional information for me to help you assist me please let me know. The project requires to be password protected, though.
I am utterly baffled at the moment because I have a functioning piece of code in this section of an uncompleted project, or so I thought. I am looking for any and all advice that will help solve this problem.
I am not getting a 'sane' result, I am doing the same thing twice and getting different results. Both pieces of code return ALL of the answers to the SAME question successfully, but one of them is displaying them in order 17, 18, 19, 16 within the exact block of code, while the second displays 16, 17, 18, 19 in proper order.
In the second piece of code as well, which is part of a more detailed view of a given question and all answers associated with it (just a more detailed view of the first code), the edit functions will edit the correct code, but the delete check boxes fail and delete the wrong item. This pattern does NOT coincide with the visual ordering of the malfunctioning code, either, and appears quite random. This may be the same or a different problem, and I am looking into this.
As follows with two sections of code, one of the sections is displaying rows of the data of all of the answers to a question:
$query2 = "SELECT * FROM ST_Answers
WHERE referencingQuestionID = '$permIDNum'
ORDER BY 'permanentAnsNumber'";
$result2 = mysql_query($query2);
$ret = "";
$letter = "a";
$ret .= "<div class='lq answerListItem'>";
$ret .= "<span class='lqh2 answerNumber'> </span>";
$ret .= "<span class='lqh2 answerTextBody'>Answer Text Body</span>";
$ret .= "<span class='lqh2 answerSummaryText'>Answer Summary Text</span>";
$ret .= "<span class='lqh2 answerNextQuestion'>Next Q#</span>";
$ret .= "<span class='lqh2 correct'>Correct?</span>";
$ret .= "</div>";
$i = 0;
$n = mysql_numrows($result2);
while( $i < $n ){
$permAID = mysql_result($result2,$i,"permanentAnsNumber");
$aText = mysql_result($result2,$i,"answerTextBody");
$aSummary = mysql_result($result2,$i,"answerSummaryText");
$nextQID = mysql_result($result2,$i,"nextQuestionID");
$correctA = mysql_result($result2,$i,"correctAnswer");
$ret .= "<div class='lq answerListItem'>";
if($letter != "a") { $ret .= "<br/>"; }
$ret .= "<span class='lq answerNumber'> </span>";
$ret .= "<span class='lq answerTextBody'>" . $letter . ") $aText </span>";
$ret .= "<span class='lq answerSummaryText'>" . $aSummary . "</span>";
$ret .= "<span class='lq answerNextQuestion'>" . $nextQID . "</span>";
$ret .= "<span class='lq correct'>" . $correctA . "</span>";
$letter++;
$ret .= "</div>";
$i++;
}
This second piece of code, is displaying the same data but a bit more using the SAME query, but instead of displaying items out of order, it displays them properly in the correct order, yet the code is almost exactly the same!
$query2 = "SELECT * FROM ST_Answers
WHERE referencingQuestionID='$permID'
ORDER BY 'permanentAnsNumber'";
$result2 = mysql_query($query2);
$ret .= "<div id='answerContainer'><h3>Associated Answers: </h3>";
$i = 0;
$n = mysql_numrows($result2);
// NOTE: This is displaying everything in correct order
// TODO: Reference this
while( $i < $n ){
$permAID = mysql_result($result2,$i,"permanentAnsNumber");
$aText = mysql_result($result2,$i,"answerTextBody");
$aSummary = mysql_result($result2,$i,"answerSummaryText");
$nextQID = mysql_result($result2,$i,"nextQuestionID");
$correctA = mysql_result($result2,$i,"correctAnswer");
$ret .= "<div class='answerRow'>";
$ret .= "<span class='perAID'>" . "<h5>Answer ID(Warning do not change):<textarea rows='1' cols='4' name='ids[]'>" . $permAID . "</textarea></span></h5>";
$ret .= "<span class='aText'>" . "Answer Text: <br /><textarea name='aTxt[]' rows='5' cols='40'>" . $aText . "</textarea></span><br />";
$ret .= "<span class='aSummary'>" . "Answer Summary: <br /><textarea name='aSum[]' rows='5' cols='40'>" . $aSummary . "</textarea></span><br />";
$ret .= "<span class='nextQID'>Question this leads to: <textarea name='nxtQID[]' rows='1' cols='4'>" . $nextQID . "</textarea></span><br />";
$ret .= "<span class='correctA'>" . "Answer point value: <textarea name='ansCorrect[]' rows='1' cols='2'>" . $correctA . "</textarea></span><br />";
$ret .= "<span class='del'><h6>Delete? <input type='checkbox' name='delete[]'></h6></span><br />";
$ret .= "</div><br />";
$i++;
}
Does anyone have any ideas on where to even begin with this? I'm entirely stumped.
Your problem is that you use quotes around a column name. Introducing that way a constant literal string to ORDER BY you effectively eliminate the ordering of the resultset.
Change
$query2 = "SELECT * FROM ST_Answers WHERE referencingQuestionID = '$permIDNum'
ORDER BY 'permanentAnsNumber'";
^ ^
to
$query2 = "SELECT * FROM ST_Answers WHERE referencingQuestionID = '$permIDNum'
ORDER BY permanentAnsNumber";
Here is SQLFiddle demo.
On a side note: learn and use prepared statements with either PDO or MySQLi. Mysql_* extension is deprecated.
I advice you change your style, try to use this
$query2 = "SELECT * FROM ST_Answers WHERE referencingQuestionID='$permID' ORDER BY permanentAnsNumber";
$result2 = mysql_query($query2);
$ret .= "<div id='answerContainer'><h3>Associated Answers: </h3>";
while($row = mysql_fetch_assoc($result2)) {
$ret .= "<div class='answerRow'>";
$ret .= "<span class='perAID'>" . "<h5>Answer ID(Warning do not change):<textarea rows='1' cols='4' name='ids[]'>" . $row["permanentAnsNumber"] . "</textarea></span></h5>";
$ret .= "<span class='aText'>" . "Answer Text: <br /><textarea name='aTxt[]' rows='5' cols='40'>" . $row["answerTextBody"] . "</textarea></span><br />";
$ret .= "<span class='aSummary'>" . "Answer Summary: <br /><textarea name='aSum[]' rows='5' cols='40'>" . $row["answerSummaryText"] . "</textarea></span><br />";
$ret .= "<span class='nextQID'>Question this leads to: <textarea name='nxtQID[]' rows='1' cols='4'>" . $row["nextQuestionID"] . "</textarea></span><br />";
$ret .= "<span class='correctA'>" . "Answer point value: <textarea name='ansCorrect[]' rows='1' cols='2'>" . $row["correctAnswer"] . "</textarea></span><br />";
$ret .= "<span class='del'><h6>Delete? <input type='checkbox' name='delete[]'></h6></span><br />";
$ret .= "</div><br />";
}
As Peterm stated, you can't have quotes in this code around the 'ORDER BY' specifier. Once fixed in this piece of code as well as a parent piece of code higher up, everything displays correctly.
Use string and variable concatenation methods while trying to use php variables inside your query or string. Quotes are not required after 'ORDERBY'. take care that too.. This changes may help you here.
change the query :
$query2 = "SELECT * FROM ST_Answers WHERE referencingQuestionID =
'".$permIDNum."' ORDER BY permanentAnsNumber";

Display a table in while loop

I am trying to display a table in while loop with 3 rows and 3 td for each row. But I am confusing how to archived thit.
This is my code so far:
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output = "<table>\n";
$output .= " <tr>\n";
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
$output .= " </tr>\n";
$output .= "</table>\n";
echo $output;
}
The above code given me a table with multiple rows and 1 td for each row. But it is not my expecting result.
Can anybody tell me how can I display such a table in my While loop?
The current code creates a new table for every single entry. What you want is to move the <table> and </table> pieces outside of the loop, then you want a counter to keep track of how many cells you've handled. If it's even, start a new <tr>. Otherwise, end the current </tr>. Make sure you check at the end to see if you've finished a </tr>, and optionally add an empty <td></td> if needed.
this aught to do it
<?php
echo "<table>\n";
$cells = $total = 0;
$total_cells = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result))
{
$testimonial = nl2br($row['testimonial']);
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
if ($cells === 0)
echo "<tr>\n";
//Create new testimonial output
$output = " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
$cells++;
$total++;
if ($cells === 3 || $total === $total_cells)
{
echo "</tr>\n";
$cells = 0;
}
}
echo "</table>\n";
?>
Try this
$i = 0;
echo "<table>\n<tr>";
while($row = mysqli_fetch_array($result)){
$testimonial = $row['testimonial'];
$cityName = $row['city_name'];
$name = $row['name'];
$imageName = $row['image_name'];
$member = $row['membership_type'];
$testimonial = nl2br($testimonial);
//Create new testimonial output
$output .= " <td>\n";
$output .= " <p>\n";
$output .= " <img src=\"".UPLOAD_DIR.$imageName."\" />";
$output .= " {$testimonial}";
$output .= " </p>\n";
$output .= " <p class=\"name\">{$name}<br />\n";
$output .= " <span>A Teacher - From {$cityName}</span></p>\n";
$output .= " </td>\n";
echo $output;
if( $i %2 == 1 )
echo "</tr><tr>\n";
$i++;
}
echo "</tr>\n</table>\n";
EDIT
In general, for displaying n cells per row, change the if statement to
if( $i % n == (n - 1) )

Categories