Database select and query issues - PHP - php

I'm very new to PHP and database queries.
I have a WordPress site with the Genesis framework installed and I'm creating a child theme from it and adding some template pages to insert data from my database. I'm creating a category page and a product page.
I'm having issues on some of the queried data on the category page, if there is anyone out there that can point me in the right direction, I'd be extremely grateful.
I have all the database rows outputting individually in a table format, which is exactly how I want it, but I can't get a heading to output above the displayed products, I've tried lots of solutions but none so far have done the job. Here is my code:
function child_product_data_loop() {
global $wpdb;
$category = $wpdb->get_results("SELECT * FROM product_data WHERE prod_cat_1='Retractable Plastic Pens';");
echo "<main class='content'>
<article class='page type-page status-publish entry' itemscope='' itemtype='http://schema.org/CreativeWork'>
<header class='entry-header'>
<h1 class='entry-title' itemprop='headline'>".$category->prod_cat_1."</h1>
</header>
<div class='entry-content' itemprop='text'>
<p>For more information on any product within our ".$category->prod_cat_1." range please click on "details and prices".</p>";
foreach($category as $product){
echo "<table width='100%' border='0' cellpadding='2' cellspacing='0'>
<tbody>
<tr valign='top'>
<td><h3><a href='' style='color: rgb(51, 102, 153); text-decoration: none;'>".$product->prod_name."</a></h3></td>
<td style='color: #ff0000; text-align: right;'>From: £".$product->prod_price_5."</td>
</tr>
<tr valign='top'>
<td colspan='2'><a href='' style='color: #CC6600; text-decoration: none;'>Click here for details and prices</a></td>
</tr>
<tr align='center' valign='middle'>
<td height='75' colspan='2'><a href='' title='".$product->prod_name."'><img width='100%' height='auto' src='http://thepensite.co.uk/wp-content/uploads/media-clic.jpg' class='prod-image' alt='".$product->prod_name."' srcset='http://thepensite.co.uk/wp-content/uploads/media-clic-300x33.jpg 300w, http://thepensite.co.uk/wp-content/uploads/media-clic.jpg 420w' sizes='(max-width: 420px) 100vw, 420px' /></a></td>
</tr>
</tbody>
</table>";
}
echo "</div>
</article>
</main>";
}
My issue is within these lines of code:
echo "<main class='content'>
<article class='page type-page status-publish entry' itemscope='' itemtype='http://schema.org/CreativeWork'>
<header class='entry-header'>
<h1 class='entry-title' itemprop='headline'>".$category->prod_cat_1."</h1>
</header>
<div class='entry-content' itemprop='text'>
<p>For more information on any product within our ".$category->prod_cat_1." range please click on "details and prices".</p>";
For some reason the category name is not been outputted, as I said before I have tried quite a few solutions I've found but they haven't worked, so I'm obviously doing some thing totally wrong.
Can any of you out there help?
Many thanks in advance.

Use $category[0]->prod_cat_1 you are not accessing the array value correctly. [0] is key here
EDIT:
Do print("<pre>".print_r($category,true)."</pre>"); after your query and look how are you getting the results.

In all likelihood, you are not accessing what you got back from the query properly. I have to guess a bit because you have not posted enough information to be certain.
Suggestion #1:
Add your table definition to the question so I can see what columns you have.
Suggestion #2: dump the data
foreach($category as $product){
var_dump($category); // <--- add this
Edit your question and post the output.
I am assuming that you are looking for html structure that is a table with all your rows inside the table. The way you have made your loop, you will end up with a table for each product.
Is this what you want?
table
product
product
product
This is what your code will produce:
table
product
table
product
table
product

Related

PHP: Three images from database in a row

I would like to display three images from the database in one row. At the moment, all the images are in a vertical line and I do not know how to fix it. Any help would be appreciated. I am only learning PHP, have checked other questions and they have been no help.
I have tried using Bootstrap however that has not worked either. The way I have it does display 3 in a row however, shoes the same product in each row and only if I have the query $show_men="SELECT model, id, price,image FROM products WHERE id='id' "
<div class="col-xs-12 col-sm-4">
<!--selecting products from database and displaying. when user clicks on more info button, information is stored in array and displayed on item.php-->
<?php
include 'mysql.php';
$show_men="SELECT model, id, price,image FROM products WHERE cat='men' ";
$query_men=mysqli_query($conn,$show_men);
while($row=mysqli_fetch_array($query_men,MYSQLI_ASSOC)){
echo '<br><br><table class="table"><tbody>
<tr>
<div><center><img class="img-responsive" src="../images/'.$row['image'].'" width="200" height="200"></center><br>
<center><strong>'.$row['model'].'</strong><br><br><strong>Price:£ '.$row['price'].'</strong><br><br>
More Info<br><br><strong></center></div><hr>
</tr>
</tbody>
</table>';
}?>
</div>
You need to iterate the column instead of the table..
<div class="row">
<?php
include 'mysql.php';
$show_men="SELECT model, id, price,image FROM products WHERE cat='men' ";
$query_men=mysqli_query($conn,$show_men);
while($row=mysqli_fetch_array($query_men,MYSQLI_ASSOC)){
echo '<div class="col-xs-12 col-sm-4"><br><br><table class="table"><tbody>
<tr>
<div><center><img class="img-responsive" src="../images/'.$row['image'].'" width="200" height="200"></center><br>
<center><strong>'.$row['model'].'</strong><br><br><strong>Price:£ '.$row['price'].'</strong><br><br>
More Info<br><br><strong></center></div><hr>
</tr>
</tbody>
</table></div>';
}
?>
</div>

Showing custom post code in post excerpt

I am building a website which will be used for posting tables in each post. For this purpose I have written a custom post code which enables the user to create tables simply by using ACF and data from database.
The thing is I would like to show the table also in the excerpt of the each post but I only get the post text written in the WP Post, written by the end user.
For now i have set it this way:
<!-- Blog Content -->
<?php echo $current_post['content'];
//Start my custom table code (HTML + PHP)?>
<table style="undefined;table-layout: fixed; width: 100%">
.
.
.
.
.
.
</table>
<?php
//End my custom table code
?>
Right now, only the content which is written by the end user ($current_post['content']) is showing in the excerpt, while in full post preview my custom table is showed normally.
What is the best way to solve this?
Thanks in advance!
$head1Title = $_POST['postTitle'];
$text = $_POST['postText'];
<table>
<tr> <th><?php echo $head1Title; ?> </th> </tr>
<tr> <td> <?php echo $text; ?></td></tr>
</table>
maybe try this one example?

Display two tables from mysql using PHP and spry or something similar

I'm trying to display data from two different tables in mysql using PHP and something like spry.. Originally, I wanted to used spry tabbed panels, but I'm not sure if that's possible. I would want the tab name to be populated from one table and the contents of that tab to be populated from another table. Here's the basic html...
<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab" tabindex="0">NAME_1 POPULATED FROM FIRST TABLE</li>
<li class="TabbedPanelsTab" tabindex="0">NAME_2 POPULATED FROM FIRST TABLE</li>
</ul>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent">
<table border="0" cellspacing="5" cellpadding="5">
<tr>
<!-- CONTENT POPULATED FROM SECOND TABLE!-->
<td width="450"> </td>
<td width="50"> </td>
<td width="50"> </td>
<td width="50"> </td>
<td width="50"> </td>
</tr>
</table></div>
</div>
</div>
</div>
The problem I think I'll have is that I won't be able to connect the tabs with the content. Since the tabs are listed first in the html... then the content. I don't know if this is possible or if there is a better way.
It depends a little on the final result you want to get. For example:
If the only thing you want is a list of tabs taken from a table and their content completed from another DB table you just need to do 2 query, one for each table, and save the results in variables. You can then complete the HTML structure inside a variable and finally do a echo of this variable at the end.
// $result1 has the info of the tab names.
// $result2 has the contents of each tab.
$final_html='<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">';
while($row1=$result1->mysqli_fetch_array()){
$final_html.="<li class=\"TabbedPanelsTab\" tabindex=\"0\">$row1[0]</li>";
}
$final_html.='</ul><div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent">
<table border="0" cellspacing="5" cellpadding="5">
<tr>';
while($row2=$result2->mysqli_fetch_array()){
$final_html.="<td width=\"450\">$row2[0]</td>";
}
$final_html.='</tr></table></div></div></div></div>';
echo $final_html;
If the problem is how to make tabs work I would suggest using Jquery.
The main idea would be to use ids based on the same variable in the tab and the tab content. For example one tab could be named 'tab1' and its contents could be named 'tab1content'.
If you want more detail in the Jquery part please post some of your code to see how do you want these tabs to work.

jquery data slide show like image slide show

i have bellow code which display 10 items and each item in each of table
<div class="baner_main shadow">
<div id="adds">
<div class="latest_ads">
<table border="0" cellspacing="0" style="width:800px">
<tbody>
<tr class="">
<?php
for($i=;$i<=10;$i++)
{
?>
<td class="even">
<img src="images/photo.gif" alt="" title=""><br>
<h3>Item Name</h3>
<p><strong>Price: 70.00 USD <br> () <br> December 3, 2011</strong></p>
</td>
<?php
}
?>
</tr>
</tbody>
</table>
</div>
</div>
</div>
in 800 width of table it only show 5 items on screen one time.
what i need is
Move the first item and bring the next five items automatically after 10 sec using Jquery.
A small dot under table to move items next and previous with jquery.
this is common in image slide shows but i can't find any idea how to do with table.
if available give me any example link and i will it myself.
Thanks
You can use jQuery cycle plugin http://malsup.com/jquery/cycle/ and check http://malsup.com/jquery/cycle/div.html for grouping set of items together. Most of the examples provided are Div based layouts. However, You can achieve the same effect using TD's as well. Let me know if you still need help

Submitting values of "jQuery Editable Invoice" into a MySQL DB using PHP

I am using this fantastic example of a jQuery editable invoice as a template for creating dynamic invoices for my users.
It's working quite well and I am successfully generating the items but I now need to save the values entered into the various text fields and enter them into the MySQL database.
I am confident in doing the MySQL entering with PHP but what makes this trickier is that the amount of 'invoice items' is completely dynamic and I am unsure how I can get PHP to 'check' through the pages text fields and find new ones, group them and then add them to my DB.
Here is an example of my code that I am using to generate the items:
<?php if($invoice_items->result_array()) { ?>
<?php foreach($invoice_items->result_array() as $invoice_Row): ?>
<tr class="item-row">
<td class="item-name">
<div class="delete-wpr">
<textarea><?php echo $invoice_Row['item_name']; ?> Facility Booking</textarea>
<a class="delete" href="javascript:;" title="Remove row">X</a>
</div>
</td>
<td class="description">
<textarea><?php echo $invoice_Row['description']; ?></textarea>
</td>
<td><textarea class="cost">$<?php echo $invoice_Row['hourly_cost']; ?>.00</textarea></td>
<td><textarea class="qty"><?php echo $total_time_hours; ?></textarea></td>
<td><span class="price">$<?php $unit_total = $invoice_Row['hourly_cost']* $total_time_hours; echo $unit_total;?>.00</span></td>
</tr>
<?php endforeach; ?>
<?php } ?>
I am thinking that I need to perhaps generate unique ID's for each invoice items text field, ie item-1-desc, item-1-cost etc, but that involves writing javascript which I know almost nothing about. Also I would still have to get PHP to loop through the ID's somehow until it reached the end...
If anyone has attempted something similar before or you can see a solution to my problem I would greatly appreciate your help.
Thanks,
Tim
Use the php form array syntax name="item-desc[<?php echo $id?>]"
You can then iterate them on the backend with foreach to store the data. You have the id's as keys to the arrays so it should be fairly trivial to update the db.

Categories