jquery simplecart php variables - php

I'm having difficulties with the onclick jquery within my php code, how do I get a php variable so that name=variable and price=variable?
<?php
$result = mysqli_query($db,"SELECT * FROM table");
while($row = mysqli_fetch_array($result)) {
echo '<ul>';
echo '<li>' . $row['selection'] . '<div style="float:right;"><a href="#" onclick="simpleCart.add('name='. $row['selection'] . ','price='. $row['price'] .');return false;
"> '. $row['price'] . '</a></div></li>';
echo '</ul>';
}
?>
How do I get around this? Thanks

change your code with this line...
echo '<div style="float:right;"><a href="#"
onclick="simpleCart.add(name='.$row['selection'].',price='.$row['price'].')"
</a></div>';

Not faniliar with simplecart, but You definitely need to crrectly escape your quotes.
<?php
$result = mysqli_query($db, "SELECT * FROM table");
while($row = mysqli_fetch_array($result)) {
echo '<ul>';
echo '<li>' . $row['selection'] . '<div style="float:right;"> '. $row['price'] . '</div></li>'; echo '</ul>'; }
?>

Related

PHP: List multiple record grouped by date

I'm trying to list multiple record from MySQL Database using PHP but when grouping it returns only one record from database, below is my PHP code I'm using.
<?php
include "connection.php";
$query = "select * from lectureupload GROUP BY submittedOn";
$result=mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$row['submittedOn'].'</h3>';
echo '</div>';
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
echo '</table>';
}
mysqli_close($conn);
?>
The current Result it gives me is somewhat like that,
My Database table is this.
Solution to this problem will be highly appreciated.
Regards.
You can't use GROUP BY clause to retrieve all records from that table. GROUP BY will always return one row per GROUP BY CONDITIONAL_COLUMN. I will suggest you below modification:
<?php
include "connection.php";
//ORDER BY submittedOn will make sure the records retrieved in ordered as per submittedOn.
$query = "select * from lectureupload ORDER BY submittedOn";
$result = mysqli_query($conn, $query);
$submitted_on_keys = array();
while ($row = mysqli_fetch_array($result)) {
if (!in_array($row['submittedOn'], $submitted_on_keys)) {
if (count($submitted_on_keys) > 0) {
//It means we have already created <table> which needs to be closed.
echo '</table>';
}
$submitted_on_keys[] = $row['submittedOn'];
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>' . $row['submittedOn'] . '</h3>';
echo '</div>';
}
echo '<tr >';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['semester'] . '</td>';
echo '<td>' . $row['teacherName'] . '</td>';
echo '<td>' . $row['lectureLink'] . '</td>';
echo '<td>' . $row['submittedOn'] . '</td>';
echo '</tr>';
}
if (count($submitted_on_keys) > 0) {
//There is last <table> which needs to be closed.
echo '</table>';
}
mysqli_close($conn);
?>
You should group it in your PHP code rather than your SQL.
Something like this:
$lectures = [];
$result = mysqli_query($conn, "select * from lectureupload");
while ($row = mysqli_fetch_array($result)) {
$lectures[$row['submittedOn']][] = $row;
}
foreach ($lectures as $submittedOn => $rows) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$submittedOn.'</h3>';
echo '</div>';
foreach ($rows as $row) {
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
}
echo '</table>';
}

SQL query not returning array

Working on getting a page to build off of an array that is returned from a DB to post a story, not sure what it is not working. The page URL looks like this: https://ohcrap.ninja/games/ps4/article.php?id=1
Here is the code that should be generating the content:
<?php
$id = $_GET['id'];
$query = mysqli_query($con,'SELECT * FROM `PS4` WHERE `id` =' .$id) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($query));
// Echo page content
echo "<div class='col s12 m12 l12'>";
echo "<div class='card small grey darken-3'>";
echo "<div class='card-stacked'>";
echo "<div class='card-content'>";
echo "$id";
echo "<span class='card-title'>" . $row['title'] . "</span>";
echo "<hr color='black'>";
echo "<P>By:<i> " . $row['author'] . "</i></P>";
echo "<P>Published: " . $row['published'] . "</P>";
echo "<br>";
echo "<P class='truncate'>" . $row['story'] . "</P>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
Your while loop is not doing anything useful, because you're immediately ending it with that ;.
while ($row = mysqli_fetch_array($query)) {
// all those echoes
}

How would I add the data from this query to an array?

Is there a way that I can put the data from the while loop into an array and output it using echo? I've tried defining an array and just appending it with .= but it does not add.
$query = "SELECT * FROM coils WHERE name like '%$term%' or
resistance like '%$term%' or
wraps like '%$term%' or
wire_one like '%$term%' or
wire_two like '%$term%' or
wire_three like '%$term%' or
wire_four like '%$term%' or
wire_five like '%$term%' or
wire_six like '%$term%'
LIMIT 25";
$prep = $db->getConnection()->prepare($query);
$result = $prep->execute();
$rowCount = $prep->rowCount();
if ($rowCount <= 0) {
echo "<script>alert('No Results, please try another search');</script>";
}
while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
echo "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
echo "<div id='search_result'>";
echo "<div id='search_title'>Name: " . $row['name'] . "</div>";
echo "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
echo "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
echo "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
echo "<div id='search_description'>" . $row['description'] . "</div>";
echo "</div>";
echo "</a>";
}
Try to add output in an array and then use implode().
$temp = array();
while($row = $prep->fetch(PDO::FETCH_ASSOC)) {
$temp[] = "<a href='coil.php?id=" . $row['uniqueid'] . "'>";
$temp[] = "<div id='search_result'>";
$temp[] = "<div id='search_title'>Name: " . $row['name'] . "</div>";
$temp[] = "<div id='search_ohms'>Resistance: " . $row['resistance'] . "</div>";
$temp[] = "<div id='search_wraps'>Wraps: " . $row['wraps'] . "</div>";
$temp[] = "<div id='search_around'>Wrapped Around: " . $row['wrapped'] . "</div>";
$temp[] = "<div id='search_description'>" . $row['description'] . "</div>";
$temp[] = "</div>";
$temp[] = "</a>";
}
echo implode(' ', $temp); //with or without space
You can get all the data in an array with:
$data = $prep->fetchAll(PDO::FETCH_ASSOC);
To display it, you can close PHP with ?>, then use the alternate syntax for nice clean HTML:
<?php foreach ($data as $row) : ?>
<a href="coil.php?id=<?= htmlentities($row['uniqueid']) ?>">
<div id='search_result'>
<div id='search_title'>Name: <?= htmlentities($row['name']) ?></div>
<div id='search_ohms'>Resistance: <?= htmlentities($row['resistance']) ?></div>
<div id='search_wraps'>Wraps: <?= htmlentities($row['wraps']) ?></div>
<div id='search_around'>Wrapped Around: <?=htmlentities($row['wrapped']) ?></div>
<div id='search_description'><?= htmlentities($row['description']) ?></div>
</div>
</a>
<?php endforeach ?>

PHP combined with HTML code

I am trying to put some php code in a code that is combined with HTML.
I have the following code:
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" [IN THIS PLACE] name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
On the [in this place] spot I want to put the following code:
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
echo 'checked';
}
I have tried it multiple times but I just can't get it to work.
You can concatenate a ternary operator like this:
$result = mysqli_query($db, "SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" ' . ($_SESSION['gebruikers_klasid'] == $record['klas_id'] ? "checked" : "") . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
You can do this many different ways, but if you want cleaner html code:
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while ($record = mysqli_fetch_array($result)) {
if ($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
$checked = ' checked';
} else {
$checked = '';
}
echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
OR...
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while ($record = mysqli_fetch_array($result)) {
$checked = ($_SESSION['gebruikers_klasid'] == $record['klas_id']) ? ' checked' : '';
echo '<div class="groepitem"><input type="radio"' . $checked . ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
Another way is to exit your echo then run the conditional statement then resume your echo... eg
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
echo '<div class="groepitem"><input type="radio" ';
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) { echo 'checked'; }
echo ' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
this should work
<?php
$result = mysqli_query($db,"SELECT * FROM klassen");
while($record = mysqli_fetch_array($result)) {
if($_SESSION['gebruikers_klasid'] == $record['klas_id']) {
$var = 'checked';
}
echo '<div class="groepitem"><input type="radio" '.$var.' name="groep" value="' . $record['klasNaam'] . '">' . $record['klasNaam'] . '</div>';
}
?>
EDIT: now session check it's inside the loop

Is there a better or more efficient way to do this?

This code is working for what I need it to do, but (in my opinion) it looks bad so I am hoping someone knows of a cleaner or more efficient way to do the same thing. I have several entries being pulled from the database and I want them to be styled identically. Only the logo and the the link name will change eventually I will add a description. Here is the code:
<div class="content">
<?PHP
while($row = $stmt->fetch())
{
$name = $row['name'];
$id = $row['id'];
$logo = $row['logo'];
$username = $row['username'];
echo "<div class=" . "Links" . ">";
echo "<div class=" . "linkImages" . ">";
echo "<br>" . "" . "<img src=" . "users/" . $username . "/images/" . $logo . " " . "width=" . "200" . " " . "height=" . "auto" . " " . "border=" . "0" . "/>" . "";
echo "</div>";
echo "<div class=" . "linkName" . ">";
echo "" . $name ."";
echo "</div>";
echo "</div>";
}
?>
</div>
You can trivially remove most of the echoes and string concatenation by switching to a HEREDOC:
while($row = $stmt->fetch()) {
echo <<<EOL
<div class="links">
yadayada
<br><a href="Profile.php?id={$row['id']}"><img src="users/{$row['username']}" etc....
yada yada yada
EOL;
Note that the lack of escapes in there, allowing for proper quotes around the tag attributes, and the {} notation on the embedded variables.
Don't use extra variable names. Instead, use the original.
Also, don't output every row with PHP. Use plain HTML and add the variable in it later:
<div class="Links">
<?=$row['name']?>
Or just echo as 1 line, no need for concatenation
echo "<div class=\"linkImages\">";
or
echo '<div class="linkImages">';
echo '<div class="content">';
while($row = $stmt->fetch()){
$name = $row['name'];
$id = $row['id'];
$logo = $row['logo'];
$username = $row['username'];
echo '<div class="Links">
<div class="linkImages">
<br><img src="users/'.$username.'/images/'. $logo .'" width="200" height="auto" border="0">
</div>
<div class="linkName">
'.$name.'
</div>
</div>';
}
echo '</div>';
Here's how I would write it:
<div class="content">
<?php
while ($row = $stmt->fetch()){
echo '<div class="Links">';
echo '<div class="linkImages">';
echo '<br /><img src="users/'. $row['username'] .'/images/'. $row['logo'] .'" width="200" />';
echo '</div>';
echo '<div class="linkName">';
echo ''. $row['name'] .'';
echo '</div>';
}
?>
</div>
Note that I removed the border="0" for the img tag - that should be done with CSS.
The short answer is yes. There is almost always a cleaner or more efficient way to do it.
How about something like this?
<div class="content">
<?PHP while($row = $stmt->fetch()) { ?>
<div class="Links">
<div class="linkImages">
<br><img src="users/<?=$row['username'] ?>/images/<?=$row['logo'] ?> width="200" height="auto" border="0" />
</div>
<div class="linkName">
<a href="Profile.php?id="<?=$row['id'] ?>><?=$row['name'] ?></a>
</div>
</div>
<?PHP } ?>
</div>
There are a lot of erroneous symbols etc in this, it comes with practice, but something like this might be worth trying
<?php
while($row = $stmt->fetch()) {
$string = "";
$string .= "<div class=\"Links\">\n";
$string .= "<div class=\"linkImages\">\n";
$string .= "<br />\n";
$string .= "<img src=\"users/". $row['name'] ."/images/" . $row['logo'] . "\" width=\"200\" height=\"auto\" border=\"0\" />\n";
$string .= "</div>\n";
$string .= "<div class=\"linkName\">\n";
$string .= "". $row['name'] ."\n";
$string .= "</div>\n";
$string .= "</div>\n";
echo $string;
}
?>
I'd recommend learning how to use the printf() family of functions.
$frame = '<img src="users/%s/images/%s" width="200" height="auto" border="0"/>';
printf($frame, $id, $username, $logo);

Categories