I'm fairly certain I'm overlooking something obvious, but here it goes.
I'm working with two tables in MySQL, one for categories and the other for works. I've successfully joined the tables together and am referencing both with mysql_fetch_array. (I am aware this is deprecated.)
I am also able to successfully display the category name once, followed by all of the works in that category.
I run into an issue, however, with wrapping a div around all of the entries in a category. The reason I want to wrap one div around the entries is I'm using collapsibles to keep the information organized. The category name serves as trigger for the collapsible, and the works are expanded/collapsed when the user clicks the category name.
(I'm also opening each image in a FancyBox, but that's irrelevant to my question.)
Here is my code:
$sql = "SELECT *
FROM gallery
LEFT JOIN gallery_categories ON gallery.category=gallery_categories.category_id
ORDER BY category ASC";
$result = mysql_query($sql) or print ("Can't join tables.<br />" . $sql . "<br />" . mysql_error());
$category = null;
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$title = stripslashes($row['title']);
$image = stripslashes($row['image']);
$timestamp = stripslashes($row['timestamp']);
if ($category != $row['category']) {
$category = $row['category'];
?>
<div class="page_collapsible" id="<?php echo $row['category_id']; ?>"><h3><div class="title"><?php echo $row['category_name']; ?></div></h3></div>
<div class="gallery-grid">
<div class="item">
<div class="content"><a class="fancybox" href="images/works/<?php echo $image ?>" title="<?php echo $title ?>, <?php echo $timestamp ?>"><img src="images/thumbs/<?php echo $image ?>" class="thumb" /><div class="thumb-border"></div></a></div>
</div>
</div>
<?php
}
}
The page_collapsible class contains the collapsible trigger. The gallery-grid class is the wrapper for the content to be collapsed. The item class contains information about each entry in the works table.
The problem I run into is the code only iterates the first row in the works table that matches the category. I have tried removing the IF statement for the category display and instead tried GROUP BY, but ran into the same problem. If I move the item class and the </div> for the gallery-grid class outside of the IF statement, each entry in the works table is displayed successfully, but the gallery-grid class wraps each subsequent category and corresponding entries. If I move the item class but leave the </div> for the gallery-grid class where it is currently, the collapsible content is blank, and the entries are outside of the collapsible, though properly displayed under their corresponding category.
Ultimately, how do I wrap all of the entries in a given category ONCE, while displaying ALL entries in a given category?
As far as I understand your question, aren't you looking for something like this?
You need to write the start/end of the wrapper only when category changes (preserve order by category). The items are written unconditionally in every loop pass.
Note if($category != null){...} is because of not closing the first wrapper and the last </div> outside the loop is for closing the last wrapper
while($row = mysql_fetch_array($result)){
$id = $row['id'];
$title = stripslashes($row['title']);
$image = stripslashes($row['image']);
$timestamp = stripslashes($row['timestamp']);
if ($category != $row['category']) {
if($category != null){
?>
</div>
<?php
}
$category = $row['category'];
?>
<div class="page_collapsible" id="<?php echo $row['category_id']; ?>"><h3><div class="title"><?php echo $row['category_name']; ?></div></h3></div>
<div class="gallery-grid">
<?php
}
?>
<div class="item">
<div class="content"><a class="fancybox" href="images/works/<?php echo $image ?>" title="<?php echo $title ?>, <?php echo $timestamp ?>"><img src="images/thumbs/<?php echo $image ?>" class="thumb" /><div class="thumb-border"></div></a></div>
</div>
<?php
}
?>
</div>
Related
I'm working on a little project and I want to be able to search my database for all items associated with my search
My problem is that when I use my code below, I get only one result rather than an array of all results that fit my query. I have seen some solutions that suggested using fetchAll() but that doesn't seem to output anything.
What the PHP query looks like.
if(isset($_POST['search'])){
if(preg_match("/^[a-zA-Z]+/", $_POST['sname'])){
$search = $_POST['sname']; //name in form
$pdo = & dbconnect();
$userid = $_SESSION['user_id'];
$sql = "SELECT * FROM movie_dets Where user_id=? and Title LIKE '%" .$search."%'";
$stmt=$pdo->prepare($sql);
$stmt->execute([$userid]);
if(!$stmt)
{
die("Database pull did not return data");
}
$row=$stmt->fetch();
}
}
Then I have a foreach loop in my html that looks like so
<div class="row">
<?php
$loop = 0;
foreach ($stmt as $row): //loop through result set ?>
<div class="column" >
<div>
<figure>
<img src="<?php echo "/.../".$row['Cover'] ?>" alt="<?php echo $row['Title']; ?> cover" />
<figcaption>
...
</figcaption>
</figure>
</div>
</div>
<?php
$loop++;
if ($loop % 4 ==0) //to display four results the wrap.
{
echo "</div> <div class='row'>";
}
?>
<?php endforeach ?>
</div>
In my database, there are two titles called frozen. This is supposed to output all rows when given the 'fr' search word. Instead, it only fetches one of them.
I want to get some data from a MySql database which has the same ID but different values. see the image (this is just a sample)
Although the venue styles are different, I want to pull all the styles with the same ID. I'm using a foreach loop to get the data from the database.
How can I improve my code to achieve what I want.
<?php
$myrows = $wpdb->get_results( "SELECT vf_venues.title, vf_venues.mainimage,
vf_venues.permalink, vf_venuestyles.slug
FROM vf_venues
LEFT JOIN vf_venuestyles ON vf_venuestyles.vid=vf_venues.vid WHERE
vf_venuestyles.vid=vf_venues.vid" );?>
<div class="venue-list venue-grid">
<?php
foreach ( $myrows as $myrow ) {
//pull the data from the DB
"<pre>"
$venueName = $myrow->title;
$mainImage = $myrow->mainimage;
$permalink = $myrow->permalink;
$slug = $myrow->slug;
$vid = $myrow->vid;
"<pre>"
?>
<li class="venue-block block">
<div class="venue-img">
<a href="<?php echo $permalink; ?>">
<img src="<?php echo $mainImage; ?>">
</a>
</div>
<div class="venue-details"><h2><?php echo $venueName; ?></h2></div>
<?php echo $slug; ?>
<?php echo $vid; ?>
</li>
<?php
}
?>
</div>
I managed to fix this by creating a for loop within the existing for loop. I then created a sql query to pull the venue styles for that venue.
I have a table called "races" in my Wordpress database.
main.php
my events page displays all of my races inside the table "races" - everything works fine here
<?php
global $wpdb;
$table_name = $wpdb->prefix . "races";
$result = $wpdb->get_results ( "SELECT * FROM races ORDER BY date_start LIMIT 4" );?>
<div class="events-schedule">
<?php
foreach ( $result as $race ) {
$title = $race->title;
$raceid = $race->race_id;
$location = $race->venue;
$icon = $race->race_icon;
?>
<div class="wrapper">
<div class="row">
<div class="small-12 medium-4 columns event-icon text-center">
<img src="<?php echo $icon; ?>">
</div>
<div class="small-12 medium-8 columns text-center">
<?php echo $id; ?>
<h3 class="tagline location"><?php echo $location; ?> </h3>
<h2><?php echo $title; ?></h2>
<div><a class="btn yellow" href="/test/?id=<?php echo $id; ?>">Apply Now</a></div>
</div>
</div>
<?php
echo '</div>';
} ?>
</div> <?php
?>
test.php I am trying to display the other fields on this individual page when you go to http://mywebsite.com/test/?id=64
All I am getting is the number "64" which is fine because that is the correct ID!
I am trying to retrieve the other fields, such as the title, venue, etc.. but not working. Am I missing something?
Thank you in advance.
<?php
$id = $_GET['id'];
echo $id;
$result = $wpdb->get_results ( "SELECT * FROM races WHERE id='$id'" );
foreach ($result as $race) {
$title = $race->title;
echo $title;
}
?>
You are probably not getting back any results, so the for loop never executes. Are you sure that id 64 actually exists?
Also it is a terrible practice to put variables from the URL directly in the query. I can inject SQL into your system as easily as appending it to the URL. You need to sanitize your input to protect yourself from attackers.
After seeing your db schema it looks like the id field is actually named race_id. So your query should be like this:
$wpdb->get_results ( "SELECT * FROM races WHERE race_id='$id'" );
Firstly remove single quote from id and make your query like this
$wpdb->get_results ( "SELECT * FROM races WHERE race_id=$id" );
for display all fields do this
foreach($result as $race){
echo $title = $race->title;
echo $field_1= $race->field_1;
echo $field_2= $race->field_2;
echo $field_3= $race->field_3;
}
Note:: put your fields in place of field_1,field_2,field_3
For example: I have a page that lists book titles stored in a database. What I want to happen is WHEN it gets to a certain book title say something different or add additional info like "ON SALE".
Is there a function that exists that allows me to manipulate data inside a loop?
I've tried a couple of things, but have not gotten the desired results.
Here's my thought process:
Query:
$sql = "SELECT *
FROM `artist`
ORDER BY artist_name ASC";
$q = $db->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
Output:
<?php while ($r = $q->fetch()); ?>
<li class="<?php echo $r['sort_status']; ?>">
<figure>
<div class="gallery-img"><img src="books/<?php echo $r['photo']; ?>" alt="picture of <?php echo $r['book_title']; ?>"></div>
<figcaption>
<h3 class="padding-fifteen-bottom"><?php $book = $r['book_title']; if($book=="A Tale of Two Cities"){echo "A Tale of Two Cities - On Sale NOW! ";} else { echo htmlspecialchars_decode(truncate($book,34));}?></h3>
</figcaption>
</figure>
</li>
<!-- end work item -->
<?php endwhile; ?>
Yes!
if($book_title == "My Title")
{
// Logic here.
}
HTML format
<?php if($book_title == "My Title"): ?>
<p>my html</p>
<?php endif;?>
I have 4 main categories in an opencart site.
I want to have a different header image for the 4 separate categories.
How can I get the current category name and put an if statement to select the image for the header?
Current header image code:
<div id="headerWrapper">
<div id="header">
<div class="div1">
<div class="div2">
<?php if ($logo) { ?>
<img src="<?php echo $logo; ?>" title="<?php echo $store; ?>" alt="<?php echo $store; ?>" />
<?php } ?>
</div>
</div>
</div>
You will need to work out if the path is set and the route is product/category to begin with, to check if you are even on a category page. Then you will need to use that information in a switch really (rather than a large if else listing). Can you give more detail on what it is you are wanting to change in the code above? Is it the logo or are you wanting to add a class to the header that you can then use to style via your stylesheet
To find out if you are in the category, use this in the index() method in catalog/controller/common/header.php
$get = $this->request->get;
$this->data['cat_id'] = false;
$this->data['cat_name'] = '';
if(!empty($get['route']) && $get['route'] == 'product/category' && !empty($get['path'])) {
$cats = explode('_', $get['path']);
$cat_id = array_pop($cats);
$cat_id = (int) $cat_id;
if($cat_id) {
$this->load->model('catalog/category');
$result = $this->model_catalog_category->getCategory($cat_id);
$this->data['cat_id'] = $cat_id;
$this->data['cat_name'] = $result['name'];
}
}
This will give you two variables to use in your template's common/header.tpl file
$cat_id and $cat_name
$cat_id will be the current category id, or false if it's not a category page
$cat_name will have the current category name if one exists, or an empty string if it doesn't