Need help showing code results in a table - php

I'm trying to get this
for ($r = 1; $r <= 10; $r++) {
if ($shirt_info[$r][0] != "Select One")
{
echo "<div class='shirtinfo'>Order $r: </div>";
echo "<div class='shirtinfo'>Size: ".$shirt_info[$r][0]."</div>";
echo "<div class='shirtinfo'>Price: ".$shirt_info[$r][1]."</div>";
echo "<div class='shirtinfo'>Qty: ".$shirt_info[$r][2]."</div>";
echo "<div class='shirtinfo'>SPE: ".$shirt_info[$r][5]."</div>";
echo "<div class='shirtinfo'>LIA: ".$shirt_info[$r][6]."</div>";
echo "<div class='shirtinfo'>Vendor: ".$shirt_info[$r][7]."</div>";
echo "<div class='shirtinfo'>Style: ".$shirt_info[$r][8]."</div>";
echo "<div class='shirtinfo'>Color: ".$shirt_info[$r][9]."</div>";
echo "<div style='clear:both;'></div>";
}
}
to show in a table and I'm not really sure how. I keep getting a syntax error.

The code you are showing us has an unterminated for loop. Maybe this is the syntax error.

Your for does not end with }
I would also suggest that you use table and not div if you want to display a table.

Related

PHP and MYSQLI with LIKE array: does not echo all content correctly

I have come this far and seem to be stuck on this trivial output issue, apologies in advance.
The $getexhibitions is an array and works when only this aspect of code is running. I have then added $results based on a LIKE keyword link that is intended to be an array within the array.
My problem is that the output is supposed to show $getexhibitions->title with the list of each $results->sponsor underneath. Java then creates a drop down with specific sponsor information. This will then repeat for the preceeding 8 years as so.
As you can see the code is getting stuck and i feel as though this may be a div or { problem or it may be more with the array code. Thanks in advance.
http://tenmoregirls.com/tenmoregirls/sponsors.php
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$query_getexhibitions = $mysqli->query("SELECT * FROM exhibitions ORDER BY year DESC");
if ($query_getexhibitions) {
while($getexhibitions = $query_getexhibitions->fetch_object()) {
echo "<a>$getexhibitions->year | $getexhibitions->title</a>", "<br />";
$sponsorlink = $getexhibitions->year;
$result = $mysqli->query("SELECT * FROM sponsors WHERE (`year` LIKE '%".$sponsorlink."%')");
if ($result) {
while($results = $result->fetch_object()) {
echo "<a href = \"javascript:void(0)\" class=\"subheading\" onclick = \"document.getElementById($results->id).style.display='block'\" >$results->sponsor</a>", "<br />";
echo "<div id=\"$results->id\" style=\"display: none;\">
<img src=\"exhibitstyles/links/close.png\" alt=\"close\" width=\"20\"/><br />";
echo '<img src="sponsorimages/'.$results->logo.'">';
echo $results->summary, "<br />";
echo "$results->website", "<br /><br /><br />";
} "</div>"; } "</div>";
}} exit(); ?>
It's probably not a problem with PHP and MYSQLI, but just with a wrong closing div tag, which is outside the loop. There is also one additional closing tag that don't seem to refer to anything. Try to move </div> tag inside the loop and delete the other one.
Edit:
You were also missing echo command before "</div>"; tag, because you used ; on the end of the previous line. You also can't use , between multiple echo parameters. . is a concatenation operator for strings in php. Whereas . is a concatenation operator for strings, echo construction also allows the usage of , between arguments. My bad.
Try this:
while($getexhibitions = $query_getexhibitions->fetch_object()) {
echo "<a>$getexhibitions->year | $getexhibitions->title</a><br />";
$sponsorlink = $getexhibitions->year;
$result = $mysqli->query("SELECT * FROM sponsors WHERE (`year` LIKE '%".$sponsorlink."%')");
if ($result) {
while($results = $result->fetch_object()) {
echo "<a href = \"javascript:void(0)\" class=\"subheading\" onclick = \"document.getElementById($results->id).style.display='block'\" >$results->sponsor</a><br />";
echo "<div id=\"$results->id\" style=\"display: none;\">
<img src=\"exhibitstyles/links/close.png\" alt=\"close\" width=\"20\"/><br />";
echo '<img src="sponsorimages/'.$results->logo.'">';
echo $results->summary . "<br />";
echo "$results->website<br /><br /><br />";
echo "</div>";
}
}
}

I want to add .html at the end or a full url fetch from Database and php query between echo

I am going to write following code for my blog. But problem, I am facing with:
"echo 'a href=".$query2['url']."</a>';" I want to add .$query2['url']. in echo and also want to add .html at the end of url like
"a href=".$query2['url'].html.".
If you have any idea please tell me as soon as possible thanks. Basically I am fetching this data from database .
<?php
$start=0;
$limit=5;
if(isset($_GET['id']))
{
$id=$_GET['id'];
$start=($id-1)*$limit;
}
$query=mysql_query("select * from tbl_services LIMIT $start, $limit");
echo "<div class='entry'>";
while($query2=mysql_fetch_array($query))
{
echo '<h2>' .$query2['name'].'</h2>';
echo "<p>".$query2['Contents']."</p>";
echo "<div class='meta'><i class='fa fa-clock-o'></i>".$query2['date']. "<b><i class='fa fa-user'></i></b> Written by <strong>Arslan Ali</strong> <b><i class='fa fa-comment-o'></i></b>" ;
echo "</div>";
}
echo "</div>";
$rows=mysql_num_rows(mysql_query("select * from tbl_services"));
$total=ceil($rows/$limit);
echo "<ul class='pagination'>";
if($id>1)
{
echo "<li><a href='?id=".($id-1)."' class='disabled'><<</a></li>";
}
for($i=1;$i<=$total;$i++)
{
if($i==$id) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li><a href='?id=".$i."'>".$i."</a></li>"; }
}
if($id!=$total)
{
echo "<li><a href='?id=".($id+1)."' class='disabled'>>></a></li>";
}
echo "</ul>";
?>
(Updated)
Replace this line
echo '<h2>' .$query2['name'].'</h2>';
with
echo "<a href='".$query2['url'].".html'>".$query2['name']."</a>";
There is still one problem with the solution made by Syntax Error. The href is in single quotes. This is not the proper Syntax. It is easily remedied by reversing the use of the single and double quotes as I have shown here.
echo '' . $query2['name']. '';

Separate MySQL results into Divs using while loop

Just beginning PHP to bear with me.
Results I'm trying to achiever:
I have a table of YouTube URL's and MetaData.
Trying to build this:
<div class="slide">
<iframe></iframe>
<iframe></iframe>
</div>
<div class="slide">
<iframe></iframe>
<iframe></iframe>
</div>
Two videos per slide, then I'm going to paginate through results using Deck.js.
I suspect I'm going about this completely the wrong way, not that experienced at programmin g logic;
while($data = mysql_fetch_array($result)) {
for ($counter = 1; $counter<=2; $counter++) {
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
/* If Video 1, increment counter for 2nd video */
if ($counter == 1) {
$counter++;
}
/* If Video 2, close div and reset counter */
else if ($counter == 2) {
echo "</div>";
$counter = 1;
}
/* If error break out */
else {
echo "</div>";
break;
}
}
}
Basically trying to nest loops to keep track of how many videos per div and start a new one when a div has two.
I've tried a few different ways, this being the latest. Results in:
<div class="slide">
<iframe></iframe>
<div class="slide>
<iframe></iframe>
Hit the blank wall now, not sure what to try next. Willing to use/learn any method to accomplish the results, just not sure where to go at this point.
Cheers.
You could remove the second loop all together using the % operator (modulus). The idea is that a % b === 0 then the number a was evenly divisible by b. Using this, you can easily check for even or odd or every Nth row.
$k = 1;
echo "<div class=\"slide\">";
while($data = mysql_fetch_array($result)) { // you should really change to mysqli or PDO
if($k % 3 === 0){
echo "</div>";
echo "<div class=\"slide\">";
}
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
$k++;
}
echo "</div>";
Put the echo <div> before the for loop (still inside the while loop) and the </div> after the for loop
In your while loop you're retrieving just one row, but then you're iterating over it twice with a nested loop. Do away with the inner loop and just use a flip-flop variable to track left and right. I think this will do what you want:
$left=true; // track whether we're emitting HTML for left or right video
while($data = mysql_fetch_array($result)) {
if ($left) {
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
}
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
if (!$left) {
echo "</div>";
}
$left = !$left; // invert $left to indicate we're emitting the right iFrame
}
// end of loop. If we had an odd number of
// videos, tidy up the HTML
if (!$left) {
echo "</div>";
}
PHPFiddle
<?php
$x = 10;
$counter = 0;
while($x > 0)
{
if($counter != 0 && $counter % 2 == 0)
{
echo "ENDOFSLIDE</br>";
}
if($counter == 0 || $counter % 2 == 0)
{
echo "SLIDE</br>";
}
echo "iframe => $x </br>";
$x--;
$counter++;
}
echo "ENDOFSLIDE";
?>
It won't work because the for loop is inside the fetch loop for the SQL data. The second iteration of the for loop does not have a new SQL row. A better solution would be to capture the common column that identifies the two videos (the title) and generate a new div whenever that value changes. Try something like this, which will work for any number of SQL rows with the same title. This will also give proper results if the SQL query returns no rows and will handle the potential of a title with only one URL - which could get ugly if you merely flip-flop and end up with URLs on the wrong title. Of course, as in your current solution, your SQL query must ORDER BY VIDEO_TITLE so the rows are adjacent. I didn't run it, but should be close.
$lastTitle = "";
$n = 0; //count SQL rows processed
while($data = mysql_fetch_array($result)) {
// See if the title changed from last
if( $data['VIDEO_TITLE'] != $lastTitle ) {
// if we processed any rows, must end the current div before starting a new one
if( $n > 0 )
echo "</div>";
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
//save the title as last title
$lastTitle = $data['VIDEO_TITLE'];
}
$n++; //count the SQL row
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
}
if( $n > 0 )
echo "</div>";

Loop and if Statement in php

I have the following code.
<?php if ( $showdata['venue'] == "Brean Sands" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Camber Sands" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Pakefield" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Prestatyn Sands" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
<?php if ( $showdata['venue'] == "Southport" ) {echo "<div class=\"gigpress-related-park\">"; echo $showdata['date_long']; echo $showdata['venue']; echo "</div>"; } ?>
This pulls the information from my db just fine, but I need it to loop, or repeat so that multiple dates appear for each venue. There are multiple dates for each venue in the db, but as the code is, it only returns the value for the first date. I need the others to appear as well.
How do I get this code to loop while looking at only one venue and pull all dates, then end?
$query = mysql_query("the query");
while($showdata = mysql_fetch_assoc($query)) {
put your code here
}
So you run the query to get all database records you want, then you loop through assigning the row to $showdata. This will then process everything inside the loop for every row it returns from the database query
assuming you use mysql_ function to fetch values from db. here is what you should do.
$query = mysql_query('SELECT QUERY');
while($showdata = mysql_fetch_assoc($query)) {
echo "<div class=\"gigpress-related-park\">" . $showdata['venue'] . $showdata['venue'] . "</div>";
}
however it would be better of using PDO or MySQLi instead of using mysql_.

Couldn't fetch mysqli...Call to a member function fetch_assoc() on a non-object

I'm totally baffled. My entire page code is below.
Originally, I had the include header.php, sidebar, topMenuBar, & mainContentShell at the top of the page, ran the first query after it, and then the second query, etc for the rest of the page. Everything worked. My first query was different though... I checked that the $_GET['stone'] number was greater than zero and less than the select max(StoneID), but I could still get errors if someone manually put in the StoneID for a stone that was deleted from inventory. I revised my $_GET validation plan, and moved it above the included files so the header() redirect would work properly. Now, my second query won't work, even though it is completely unchanged.
Var_dump($querySN) yields string(53) "select StoneName from stonetypes where StoneID = '1' " and var_dump($resultSN) yields NULL.
It states: error occurred at line 35 --- $resultSN = $db->query($querySN);
States several times: Couldn't fetch mysqli
States a number of times: Property access is not allowed yet
And states in conclusion: Call to a member function fetch_assoc() on a non-object on line 36---$rowSN = $resultSN->fetch_assoc();
Does anyone know what's going on here and how I can fix it? Page code follows. Thanks!!!
<?php
require('./inc/config.inc.php');
$stone = (INT)$_GET['stone'];
require(MYSQL1);
$queryCk = "select StoneID from stonetypes";
$resultCk = $db->query($queryCk);
$var = array();
while ($rowCk = $resultCk->fetch_assoc()){
$var[] = $rowCk['StoneID'];
}
if(!in_array($stone, $var)) {
header('Location: beadgallery.php?type=stones');
exit;
} else {
include('inc/header.inc.php');
include('inc/sidebar.inc.php');
include('inc/topMenuBar.inc.php');
include('inc/mainContentShell.inc.php');
?>
<div id="mainContent">
<div class="center">
<?php
$querySN = "select StoneName from stonetypes where StoneID = '$stone' ";
$resultSN = $db->query($querySN);
$rowSN = $resultSN->fetch_assoc();
echo '<table id="cartDisplayTable">';
echo '<tr>';
echo '<td colspan="2">';
echo '<table id="titleTable">';
echo '<tr>';
echo '<td id="stoneTitle">';
if (isset($rowSN['StoneName'])){
echo '<h2>'.ucwords($rowSN['StoneName']).'</h2>';}
echo '</td>';
echo '</tr>';
$query = "select * from organized_inventory2 where StoneID = '$stone' ";
$result = $db->query($query);
$num_beadItems = $result->num_rows;
$justused='abc';
for ($i=0; $i < $num_beadItems; $i++) {
$row = $result->fetch_assoc();
if (!isset($row['itmphoto'])) {
echo '</table>';
echo '</td>';
echo '</tr>';
echo '<tr><td colspan="2"><hr id="cartDivider"></td></tr>';
echo '<tr>';
echo '<td id="cartImgCell">';
echo '<img src="img/nophoto.gif">';
echo '</td>';
echo '<td id="cartInfoCell">';
echo '<table id="innerTable">';
include ('inc/stoneCartInfo.inc.php');
} elseif ($row['itmphoto'] == $justused) {
echo '<tr><td colspan="2"><hr id="itemDivider"></td></tr>';
include ('inc/stoneCartInfo.inc.php');
} else {
echo '</table>';
echo '</td>';
echo '</tr>'
;
echo '<tr><td colspan="2"><hr id="cartDivider"></td></tr>'
;
echo '<tr>';
echo '<td id="cartImgCell">';
echo '<img src="img/invent/'.$row['itmphoto'].'">';
$justused = $row['itmphoto'];
echo '</td>';
echo '<td id="cartInfoCell">';
echo '<table id="innerTable">';
include ('inc/stoneCartInfo.inc.php'); }
}
echo '</table>'
;
echo '</td>';
echo '</tr>'
;
echo '</table>'
;
}
$result->free();
unset($result);
$db->close();
?>
</div> <!-- div class="center" -->
</div> <!-- div id="mainContent" -->
Integers don't nee quotes around the value as in StoneID = '1', this shouldn't be a problem because MySQL should typecast.
You have not checked what "$result" contains, if it's boolean then the query failed and you need to see the output of mysqli_error.

Categories