view_products() below displays only one data from my table product which contains 20 data. Depending on where you place return $output; either inside while loop which displays the first data or outside the while loop which displays the last data.
<?php echo view_products(); ?>
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "password#edadmin";
$dbname = "estore";
$dbconn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
//Test if connection occurred,
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")");
}
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($products = mysqli_fetch_assoc($result)) {
$output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<i class=\"ion ion-eye\"></i>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p>" . $products['ProductName'] . " </p>";
$output .= "<p>" . "₦ " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
return $output;
}
} else {
return "No product yet";
} // return $output;
}
The reason is that you are resetting the content of $output in the first line of your loop $output = "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
So if you put the return at the end of the loop, in the first loop it will return the first record and exit the function, if you put it at the end of the function, in each loop $output will be cleared and the content of that loop will only be written in $output so at the end of the function you will only have the content of the last loop in $output
What you can to is to set $output to an empty string and then just append everything in your loop.
Also set the value of $output in the else block and then at the end return $output
function view_products()
{
global $dbconn;
$sql = "SELECT * FROM product";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
$output = "";
while ($products = mysqli_fetch_assoc($result)) {
$output .= "<div class=\"col-lg-4 col-md-6 portfolio-item filter-app wow fadeInUp\">";
$output .= "<div class=\"portfolio-wrap\"><figure>";
$output .= "<img src=" . $products['ProductImage'] . " class=\"img-fluid\" alt=\"\">";
$output .= "<i class=\"ion ion-eye\"></i>";
$output .= "</figure>";
$output .= " <div class=\"portfolio-info\">";
$output .= "<p>" . $products['ProductName'] . " </p>";
$output .= "<p>" . "₦ " . $products['ProductAmount'] . "</p>";
$output .= "</div></div></div>";
}
} else {
$output = "No product yet";
} // return $output;
return $output;
}
Related
I have a mysql table which I'm trying to turn into jQuery listview widgets. The part that I'm having trouble with is how best to extract the info from the table without doing multiple queries on the db itself. I have the following code which works:
global $conn;
$sql = "SELECT `id` FROM `implantFamilies` WHERE `familySelector` = '" . $_POST['implantFamily'] . "'"; //need to sanitise this
$result = $conn->query($sql);
if ($result->num_rows == 1) {
while($row = $result->fetch_assoc()) {
$familyId = $row["id"];
}
} else {
die("Error: " . $result->num_rows . " family groups in result. Alert administrator.");
}
?>
<form class="ui-filterable">
<input id="filterBasic-input" data-type="search">
</form>
<?php
$sql = "SELECT DISTINCT `familySection` FROM `implants` WHERE `familyGroupId` = '" . $familyId . "'";
$sections = $conn->query($sql);
if ($sections->num_rows > 0) {
while ($sectionRow = $sections->fetch_assoc()) {
$output .= "<b>" . $sectionRow["familySection"] . "</b><br>";
//DISPLAY COLLAPSIBLE DIV HERE
$sql = "SELECT DISTINCT `sectionHeading` FROM `implants` WHERE `familyGroupId` = '" . $familyId . "' AND `familySection` = '" . $sectionRow["familySection"] . "'";
$dividers = $conn->query($sql);
if ($dividers->num_rows > 0) {
while ($headingRow = $dividers->fetch_assoc()) {
$output .= "<i>" . $headingRow["sectionHeading"] . "</i><br>";
//DISPLAY list-divisers DIV HERE
$sql = "SELECT `reference`, `description`, `filterText`, `requiresLot` FROM `implants` WHERE `familyGroupId` = '" . $familyId . "' AND `familySection` = '" . $sectionRow["familySection"] . "' AND `sectionHeading` = '" . $headingRow["sectionHeading"] . "'";
$implants = $conn->query($sql);
if ($implants->num_rows > 0) {
while ($implantRow = $implants->fetch_assoc()) {
$output .= $implantRow["description"] . "<br>";
//DISPLAY implants DIV HERE
}
} else {
$output = "0 results";
}
}
} else {
$output = "0 results";
}
}
} else {
$output = "0 results";
}
$conn->close();
echo $output;
On the first lot of (simple) data which I've put into the table, this code executes 23 mysql queries. There must be a simpler way!
I'm able to sort the second tier while loop for obvious reasons but I cannot get the first one to sort. I know its cause the "for" loop is incrementing. What I want is alphabetically sort first while loop then the second ASC...any suggestions? Here's my code
function get_content() {
$sql1 = "SELECT * FROM category";
$res1 = mysql_query($sql1) or die(mysql_error());
$total = mysql_num_rows($res1) or die(mysql_error());
for($a = 1; $a <= $total; $a++) {
$sql = "SELECT * FROM weblinks INNER JOIN category ON category_weblinks = id_category WHERE id_category = '$a' AND status_weblinks = 'checked' ORDER BY title_weblinks ASC";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n\n\n" . '<div class="post">' . "\n";
echo '<div class="title">' . "\n";
echo '<h2><a name="' . $row['shortcut_category'] . '">' . $row['title_category'] . '</a></h2>' . "\n";
echo '<p><small>Posted by Joe email</small></p>';
echo '</div>' . "\n";
echo '<div class="entry">' . "\n";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "\n" . '<p><b>' .$row['title_weblinks']. '</b><br>' . "\n";
echo $row['description_weblinks']. '<br>' . "\n";
echo 'Link: ' .$row['link_weblinks']. '<br>' . "\n";
echo 'User: ' .$row['username_weblinks']. ' | Password: ' .$row['password_weblinks']. '</p>' . "\n";
}
echo '<p class="links"> Back to Top</p>';
echo '</div>';
echo '</div>';
}
}
}
I can't put a table around my results from my code and need some help as I've tried but it comes back with "Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\search_go.php on line 27". So could I get help with how to insert a table?
<?php
//capture search term and remove spaces at its both ends if the is any
$searchTerm = trim($_GET['keyname']);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "localhost"; //server
$db = "calendar"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT * FROM caltbl WHERE evtDate LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
<table>
/* check whether there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
<tr>
$output .= "date: " . $row['evtDate'] . "<br />";
$output .= "Name: " . $row['patient'] . "<br />";
$output .= "Course: " . $row['patientId'] . "<br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchTerm;
?>
You can't just insert a HTML tag inside PHP code:
You can however just use an echo to send it out directly:
echo "<table>";
while($row = mysqli_fetch_array($results))
{
$output = "<tr>";
// <tr> This is the problem line.
$output .= "<tr>";
$output .= "<td>date: " . $row['evtDate'] . "<br /></td>";
$output .= "<td>Name: " . $row['patient'] . "<br /></td>";
$output .= "<td>Course: " . $row['patientId'] . "<br /></td>";
$output .= "</tr>";
echo $output;
}
Additionally you didn't close your <tr>. I added some extra snippets to make each field a TD in the table and then closed the row.
I have developed a search function which finds patients by their forename and surname and displays the results. However, after implementing the PHP code, the search results are not displaying.
Please note: The error messages are not displaying either;
Does anyone have any idea's why it is not displaying the search results?
<html>
<h1>Search By Name</h1>
<form action="" method="get">
<label>Name:
<input type="text" name="keyname" />
</label>
<input type="submit" value="submit" />
</form>
</body>
</html>
<?php
//capture search term and remove spaces at its both ends if there is any
if(isset($_GET['submit'])){
if(!isset($_GET['keyname'])){
$_GET['keyname'] = "";
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//check whether the name parsed is empty
if($searchTerm == "")
{
echo "Enter name you are searching for.";
exit();
}
//database connection info
$host = "localhost"; //server
$db = "a&e"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE 'Forename' = '$keyname' OR 'Surname' = '$keyname'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "PatientID: " . $row['PatientID'] . "<br />";
$output .= "Forename: " . $row['Forename'] . "<br />";
$output .= "Surname: " . $row['Surname'] . "<br />";
$output .= "Gender: " . $row['Gender'] . "<br />";
$output .= "Illness: " . $row['Illness'] . "<br />";
$output .= "Priority: " . $row['Priority'] . "<br />";
$output .= "Patient History: " . $row['Patient_History'] . "<br /><br />";
}
echo $output;
}
else {
echo "There was no matching record for the name " . $searchTerm; }
}
}
?>
Tried to post this on your previous question. If you want people to answer these you'll have to leave them up long enough for people to answer.
<?php
$form = "<html>
<h1>Search By Name</h1>
<form method=\"get\">
<label>Name:
<input type=\"text\" name=\"keyname\" />
</label>
<input type=\"submit\" value=\"Search\" />
</form>
</body>
</html>";
//capture search term and remove spaces at its both ends if there is any
if(!empty($_GET['keyname'])){
$keyname = $_GET['keyname'];
$searchTerm = trim($keyname);
//database connection info
$host = "localhost"; //server
$db = "a&e"; //database name
$user = "root"; //dabases user name
$pwd = ""; //password
//connecting to server and creating link to database
$link = mysqli_connect($host, $user, $pwd, $db);
//MYSQL search statement
$query = "SELECT PatientID, Forename, Surname, Gender, Patient_History, Illness, Priority FROM patient WHERE Forename LIKE '%$searchTerm%' OR Surname LIKE '%$searchTerm%'";
$results = mysqli_query($link, $query);
/* check whethere there were matching records in the table
by counting the number of results returned */
if(mysqli_num_rows($results) >= 1){
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "PatientID: " . $row['PatientID'] . "<br />";
$output .= "Forename: " . $row['Forename'] . "<br />";
$output .= "Surname: " . $row['Surname'] . "<br />";
$output .= "Gender: " . $row['Gender'] . "<br />";
$output .= "Illness: " . $row['Illness'] . "<br />";
$output .= "Priority: " . $row['Priority'] . "<br />";
$output .= "Patient History: " . $row['Patient_History'] . "<br /><br />";
}
}else{
$output = "There was no matching record for the name " . strip_tags($searchTerm);
}
} else {
$output = "Enter name you are searching for.";
}
echo "$form\n$output";
?>
You should put your search code into a block that gets run only if the search term has a value:
if( empty($searchTerm) )
{
echo "Enter name you are searching for.";
}
else
{
// run your search code here and display the result.
}
I have an area on my page of coloured swatches (and the tile AVAIALBLE COLOURS) that i only want to display if there is a result for them in the database.
here is my code:
private function getWTVariationHighlights($productId, $variationId)
{
$output_str = "";
$output_str .="<div class=\"mainProductSwatchesTitle\"><b>AVAILABLE COLOURS</b>\n";
$output_str .= "<div class=\"mainProductSwatches\">\n";
$sql = "select voptionid, vovalue from [|PREFIX|]product_variation_options where vovariationid=$variationId and (voname='Colour' or voname='Color')";
$result = $this->db->Query($sql);
while ($row = $this->db->fetch($result)) {
$sql = "select * from [|PREFIX|]variation_option_highlight where variation_option_id=" . $row['voptionid'] . " and product_id=" . $productId;
$result2 = $this->db->Query($sql);
if($row2 = $this->db->fetch($result2)){
if($row2['thumb_location']){
$output_str .= "<a href=\"#\" onmouseover=\"changeMain('" . $row2['location'] . "')\"><img src=\""
. $GLOBALS['ShopPath'] . "/product_images/hfh_highlight_images/" . $row2['thumb_location'] . "\" /></a>\n";
}
}
}
$output_str .= "</div>\n";
$output_str .= "<script type=\"text/javascript\">\n";
$output_str .= "function changeMain(src)\n";
$output_str .= "{\n";
$output_str .= "document.getElementById('phthumb').src = '" . $GLOBALS['ShopPath'] . "/product_images/hfh_highlight_images/' + src;\n";
$output_str .= "}\n";
$output_str .= "</script>\n";
return $output_str;
}
simple use css
div:empty
{display:none;}