I have searched many forums including Stack Overflow and not been able to find a solution for this that works. Please do not mark this as duplicate.
I have a database of users with multiple entries for certain items. The users are saved in one table ("TABLE 1") and the items are saved in another ("TABLE 2"). What I need the code to do is search TABLE 2 for user IDs that match the selected user from TABLE 1 and then for each matching entry display the item data for that row. The code I currently have is below but it is only displaying one result and then stopping. Any help is greatly appreciated.
<?php
$queryItems = mysql_query("SELECT * FROM items WHERE user_id='$results[id]'");
$itemmatch_result = mysql_fetch_array($queryitems) or die($itemmatch."<br/><br/>".mysql_error());
{
?>
<div class="column6">
<div class="item-container">
<p class="itemname"><?php echo $itemmatch_result['item_make'] . " BRAND " . $itemmatch_result['item_type']; ?> item</p>
<p class="itemsize"><strong>Item Size:</strong> <span><?php echo $itemmatch_result['item_size']; ?></span></p>
<p class="tiresize"><strong>Item Model:</strong> <span><?php echo $itemmatch_result['item_model']; ?></span></p>
<p class="tiresize"><strong>Registered:</strong> <span><?php echo date('F jS, Y', $itemmatch_result['item_registered']); ?> AT STORE</span></p>
<span class="purchase-data">BOUGHT <?php echo date('F jS, Y', $itemmatch_result['item_bought']); ?></span>
</div>
<?php
}
unset($itemmatch_result);
?>
You need to loop through all records as long as the query is giving you the records you expect.
while ($data = mysql_fetch_array($queryItems)) {
echo '
<p class.........>' . $data['item_make'] . ' BRAND .....
';
}
Btw, you shouldn't be using mysql anymore as it's deprecated, I would suggest starting to use mysqli/pdo instead.
Related
Basically, I am trying to display ratings (using kk star rating plugin) for different past bookings in my plugin. I am using do_shortcode() for different bookings with different ids. This is being echoed in a partial file.
<?php
/* GIVE RATINGS IF NOT ALREADY GIVEN */
if ($ratings_possible) {
echo do_shortcode("[kkstarratings id=" . $booking->id . "]");
?>
Here $booking->id is giving a unique booking id number based on which I am trying to get unique rating for each booking.
Issue
Currently, main file runs loop three times and this snippet does display stars perfectly, but of those three star-bar, last one is actually working. Last one accepts the ratings and updates the average, but the first two are not working. We can hover over all there stars-bar and click, but the only last one of it is actually working.
I have also used kk_star_ratings() method but same result.
More details
The following is a part of dashboard.php code. It is displaying every appointment/booking of a customer. Here $customer->future_bookings is displaying upcoming bookings (where obviously $ratings_possible is set to false). However, on $customer->past_bookings I have set $ratings_possible to true. Followed by the partial file that is responsible for each booking detail i.e. include('_booking_tile.php');
dashboard.php
<div class="customer-bookings-tiles">
<?php
foreach($customer->future_bookings as $booking){
$editable_booking = true;
$ratings_possible = false;
include('_booking_tile.php');
} ?>
</div>
<?php } ?>
<?php
if($customer->past_bookings){ ?>
<div class="latepoint-section-heading-w">
<h5 class="latepoint-section-heading"><?php _e('Past '.$appointterm, 'latepoint'); ?></h5>
<div class="heading-extra"><?php printf( __('%d '.$appointterm, 'latepoint'), count($customer->past_bookings)); ?></div>
</div>
<div class="customer-bookings-tiles">
<?php
foreach($customer->past_bookings as $booking){
$editable_booking = false;
$ratings_possible = true;
include('_booking_tile.php');
} ?>
</div>
Now, on the _booking_tile.php, it contains details of each booking like agent, time, status etc. Through $booking object we are getting the variables of a particular booking and their values and everything. Here is some part of code:
_booking_tile.php
<div class="customer-booking-info-row">
<span class="booking-info-label"><?php _e($agentterm, 'latepoint'); ?></span>
<span class="booking-info-value"><?php echo $booking->agent->full_name; ?></span></div>
<div class="customer-booking-info-row">
<span class="booking-info-label"><?php _e('Status', 'latepoint'); ?></span>
<span class="booking-info-value status-<?php echo $booking->status; ?>"><?php echo $booking->nice_status; ?></span></div>
</div>
<?php if ($editable_booking) { ?>
<div class="customer-booking-buttons">
<a href="<?php echo $booking->ical_download_link; ?>" target="_blank" class="latepoint-btn latepoint-btn-primary latepoint-btn-link">
<i class="latepoint-icon latepoint-icon-ui-83"></i>
<span><?php _e('Add to Calendar', 'latepoint'); ?></span>
</a>
<?php /* <i class="latepoint-icon latepoint-icon-ui-46"></i><span><?php _e('Edit', 'latepoint'); ?></span> */ ?>
<a href="#" class="latepoint-btn latepoint-btn-danger latepoint-request-booking-cancellation latepoint-btn-link" data-route="<?php echo OsRouterHelper::build_route_name('bookings', 'request_cancellation'); ?>">
<i class="latepoint-icon latepoint-icon-ui-24"></i>
<span><?php _e('Cancel', 'latepoint'); ?></span>
</a>
</div>
<?php } ?>
<?php /* GIVE RATINGS IF NOT ALREADY GIVEN */
if ($ratings_possible) {
echo do_shortcode("[kkstarratings id=" . $booking->id . "]");
?>
<?php } ?>
Resulting HTML code is perfectly fine, every rating-star (kk star plugin) has perfect code it has unique IDs meaning it should work fine. Moreover, as I said before when I insert three shortcodes with different IDs, it works fine.
That stars are displaying perfectly, but only the third one is actually working. When I click third one it records my rating and displays the average. While first two are not.
Stars work when I insert their shortcodes manually through WordPress. So it means that multiple shortcodes with IDs is possible.
Author provides kk_star_rating() function that can be used in the code, but it gives same result.
Only the last star is working others are just not recording anything. All three have same resulting HTML code except for the IDs which will be unique.
I will be happy to answer more of your questions.
Here's the screenshot, Third one is giving me the result when I click. But I can only hover over the first two and click multiple times with no result.
Screenshot
It was some sort of problem with the plugin. It was not compatible with newest version of WordPress. Used a different plugin and it worked fine. Calling multiple shortcodes through a loop worked perfectly.
Thanks to everyone who tried to help.
Hey I've recently been making a website and want to display the data from my database in a grid format opposed to it just listing down the page.
Here is my code right now:
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
I was wondering how I would go about creating a for loop to allow the data from this database in conjunction with the image to span across the page with 7 columns and however many rows down until it reaches the end of the database.
Thanks!
<?php
$query = "Select * from tablename";
$bind = $conn->query($query);
if ($bind->num_rows > 0){
while ($row = $bind->fetch_assoc()){
?>
<p>
<a href="pokemondetails.php?dex=<?php echo $row['dex'];?>">
<?php echo $row['name']; ?>
<br>
<img src="assets/<?php echo $row['dex']?>.png">
</a>
</p>
<?php
}
}
?>
Try this, I just add while loop until End Of file (EOF table)
I have a simple ticket sale registration system with 6 types of tickets and 3 different "physical" sales locations. Each order is inserted as a row in the database and stores the amount of each type of ticket along with the sale location, total cost and a datetime timestamp.
I want to display the total amount of each ticket type, that was sold within a given time frame along with the total cost of those tickets. I also want to filter the results based on sale location.
This is my db query:
"SELECT SUM(ticketType1), SUM(ticketType2), SUM(ticketType3),
SUM(ticketType4), SUM(ticketType5), SUM(ticketType6),
SUM(cost), saleLocation
FROM `orders`
WHERE time BETWEEN '2018-07-10 07:00:01'
AND '2018-07-11 07:00:00'"
Then I display it in a HTML table row:
<?php
while ($row = $result->fetch_assoc()) {
if($row["saleLocation"] == 'location1') { ?>
<div class="cell">
<?php echo $row["SUM(ticketType1)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType2)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType3)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType4)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType5)"] ?>
</div>
<div class="cell">
<?php echo $row["SUM(ticketType6)"] ?>
</div>
<div class="cell">
<?php echo number_format($row["SUM(cost)"], 0, "", "."); ?>
</div>
<?php } //end if
} // end while
?>
It works, but I'm trying to use an IF statement inside the WHILE loop to filter the result based on location, but the IF statement doesn't appear to have any effect and instead it displays the results from all 3 locations.
I know I can easily modify the query to achieve this, but I would rather not do that in this specific case.
I'm guessing that the problem still lies within the query, though?
Some help would be greatly appreciated.
Hello first of all what i am doing in , i am coding a website for advertise .
Now what do i need is a help to display a lots of data from two tables of database .
What i have done so far u can check at My project you have to login use (Username : test , password : 123456a) to login , so there is everything is okay except an image image are the same on every ads and i do not find the way to make it right .
So i have a "posts" table with an information about ads and an "images" table with a path of an image this is how its looks like :
and this is my code :
<?php
$userid = $_SESSION["userid"];
$sql = "SELECT * FROM posts WHERE userid='$userid' ";
$res = mysqli_query($connect,$sql);
while ($row = mysqli_fetch_assoc($res)) {
?>
<div id="ads">
<div id="titlepic">
<?php echo $row["title"]; ?><br>
<img src="<?php echo $Photo[0]; ?>" height="100px;">
</div>
<div id="managead">
Edit<br style="margin-bottom: 5px;">
Delete<br style="margin-bottom: 5px;">
Renew
</div>
<div id="dates">
<b>Date Added:</b> <?php echo date('m/d/Y', $row["dateadded"]); ?><br>
<b>Renew Date:</b> <?php if($row["renewdate"] > 0){ echo date('m/d/Y', $row["renewdate"]); } ?><br>
<b>Location:</b> <?php echo $row["location"]; ?><br>
<b>Price:</b> <?php echo $row["price"]; ?><br>
</div>
</div>
<hr width="100%">
<?php
so the question is how to extract and images from other table at the same time or how tu run two query at the same time and get an information from them
your SQL statement needs a JOIN in order to include data from two tables in one query.
$sql = "
SELECT *
FROM posts p
JOIN images i
ON p.id = i.postid
WHERE p.userid='$userid'
";
this result set will be populated with all columns from both tables. now you can access path1 via:
<?php echo $row["path1"]; ?>
while this will work for all of your images, such as $row["path2"], $row["path3"], etc, keep in mind this is a bad design for a many-to-many relationship, so it should be normalized to include a linking table which would hold all of your images.
I have a post system in place
<?php
/**
Display the results from the database
**/
$q = ("SELECT * FROM threads ORDER BY posted");
$r = mysql_query($q);
if(mysql_num_rows($r)>0): //table is non-empty
while($row = mysql_fetch_assoc($r)):
$net_vote = $row['votes_up'] - $row['votes_down']; //this is the net result of voting up and voting down
?>
<div class='entry'>
<span class='link'>
<?php echo $row['author']; ?>
<?php $row['posted'] = date("jS M Y h:i",$row['posted']); echo $row['posted']; ?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $row['message']; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<br/>
<div class='message'><?php echo $row['message']; ?><br/></div>
<?php echo "<a href='msg.php?id=$row[id]'/> Comments/Add comments $row[replies]</a>" ?>
<?php echo "Likes: " . $row['votes_up'] . "  "; echo "Dislikes: " . $row['votes_down'] . " "; ?>
</span>
<span class='votes_count' id='votes_count<?php echo $row['id']; ?>'></span>
<span class='vote_buttons' id='vote_buttons<?php echo $row['id']; ?>'>
<a href='javascript:;' class='vote_up' id='<?php echo $row['id']; ?>'></a>
<a href='javascript:;' class='vote_down' id='<?php echo $row['id']; ?>'></a>
<br/>
</span>
</div>
<br/>
<?php
endwhile;
endif;
?>
I want to add text that says 'order by : Most recent | Most liked | least liked '
As you can see i think ive got it already posting most recent by defualt which is what i want.
But what i want also is when you click 'Most liked' & 'least liked' it sorts by 'Vote_up' ( likes) & 'vote_down' (dislikes) all on the same page and shows posts with most likes on them (most liked) and most dislike (least liked)
EDIT***
sorry my question is how can i add 2 functions that when on click sorts by 'most liked' and 'least liked'
in html:
<a href='script.php?order=recent'>Recent</a>
<a href='script.php?order=liked'>Liked</a>
...
in php:
if ($_GET['order'] == 'recent') $order = "posted";
elseif ($_GET['order'] == 'liked') $order = "smth";
...
$q = "SELECT * FROM threads ORDER BY ".$order."";
but actually it's better to use some js framework (i prefer extjs for that) to sort the output on client side
AFAIK you have few choices how to do this:
Have the links pass a $_GET value via URL (causes a page refresh!!), then test for this in the PHP and run the necessary SQL to get the new record order.
Use AJAX to perform the same request asynchronously, with a PHP script to handle the SQL function and return the desired results.
Use a jQuery plugin such as tablesorter to (probably) basically provide the same functionality as that in 2, or via it's own filtering system -- I'm not sure i've not used it!!
My preference would be 1. (i.e a pure PHP + MySQL solution) as this offers the best universal functionality. You could always add javascript / ajax later to make things more swish for more modern browsers and users!