I've the following query
As you see jigsaw repeats twice because the movie has two categories
but i would like to echo both categories but not twice the movie..
<?php
while ($mInfo = $testquery->fetch(PDO::FETCH_ASSOC)) {
?>
<div class="col-xs-6 col-sm-4 col-md-3">
<a href="movie.php?title=<?php echo $mInfo['titleEN']; ?>&m=<?php echo $mInfo['imdbID']; ?>" title="<?php echo $mInfo['titleEN']; ?>" alt="<?php echo $mInfo['titleEN']; ?>" target="_self">
<div class="movie-info">
<img src="assets/images/poster/<?php echo $mInfo['poster']; ?>.jpg" alt="<?php echo $mInfo['titleEN']; ?>">
</div>
<div class="movieinformation">
<div class="movie-title"><?php echo $mInfo['titleEN']; ?></div>
<div class="movie-categories">I WOULD LIKE TO ECHO THE CATEGORIES HERE</div>
</div>
</a>
</div>
<?php
}
?>
So far i just could do it, could anyone help me with that?
Here's a concept for such tasks. I kept it with general names and left out html on purpose.
$oldMainId = null; // Initialize to null to start with. This will be needed later.
while ($item = $result->fetch()) { // get your items (movies in your case)
if($oldMain != $item['mainId']) {
// only show title if you haven't yet
echo $item['mainTitle'];
}
// always show the category though
echo $item['sub'];
// re-set the 'old' variable.
$oldMainId = $item['mainId'];
}
I would use
OPTION 1
$res = $stmt->fetchAll(PDO::FETCH_GROUP);
Then it will group on the first column, assuming that is movie ID, or something unique to the movie you would get multiple rows in a nested array for that movie.
Then when you iterate thorough them you can loop over the nested array for the genre stuff
OPTION 2
Another option is to use GROUP_CONCAT and group by the movie id,
SELECT GROUP_CONCAT(genre) AS genre, ... WHERE ... GROUP BY movieID
But be aware that GROUP_CONCAT does have a setting for max length and it will silently truncate your data if it exceeds it.
OPTION 3
You can build the structure yourself (same as fetch group does)
$data = [];
while ($mInfo = $testquery->fetch(PDO::FETCH_ASSOC)) {
$key = $mInfo['movieid']
if(!isset($data[$key])) $data[$key] = [];
$data[$key][] = $mInfo;
}
Then go through that and do your html by using a second foreach($data as ...) It has to be done after organizing the data as the order of the result is unknown.
Related
This question already has answers here:
Echo current number of row
(3 answers)
Closed 1 year ago.
I have a query that I make using PHP and it returns me all the rows that satisfied the condition that was given, now, I'm wondering if there's a way to count each number of row a result of a query has and pass that number to an id of an element on HTML?
<?php
$res2 = mysqli_query($con,"SELECT NAME, PRICE FROM product WHERE AVAILABLE = 1 AND ID_CATEGORY = 17");
?>
<body>
<?php
while($row = mysqli_fetch_row($res2)) {
print_r($row);
?>
<div class="item">
<img class="trj_img_cls" src="RSC/COMING_SOON.jpg" alt="" id="imgDG<?php ?>">
<div class="texto_trj">
<div class="nombre_producto">
<h2 id="ttlDG<?php ?>">título</h2>
</div>
<div class="precio_producto">
<i class="icon-dollar"></i>
<label id="prDG<?php ?>">0.00</label>
</div>
<div class="caracteristicas">
<ul id="caractDG<?php ?>"></ul>
</div>
</div>
</div>
<?php
}
?>
</body>
I put <?php ?> because there I want to add the variable (number of the row) so it complements the actual id and then i'm able to look for that id with javascript. Another thing is that I use the print_r() just to see if the elements were appearing.
This can be done by setting an $increment variable to 1 at the start of the while loop, and incrementing it at the end of the loop.
<?php
$increment = 1;
while($row = mysqli_fetch_row($res2)) {
And then using it like this:
id="imgDG<?php echo $increment; ?>">
And then incrementing it at the end:
<?php
$increment++;
}
?>
Hey there guys/girls I have an issue I'm currently trying to work through being a novice to MYSQL / PHP. Currently I'm using Bootstrap accordion collapsible components to display HTML tables (That are reports). Here is my current table:
Current Table in MYSQL.
So as you can see the reports row contains some HTML information which are tables. I wanted to take the information and display it on a webpage assuming that every row was a different report. So I was able to do so with writing this:
<div class="accordion" id="accordionExample">
<?php
require('db.php');
$i = 0;
$sql = "SELECT `report` FROM `automation-reports`;";
$query = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($query))
{
foreach($row as $key => $value)
{
?>
<div class="card">
<div class="card-header" id="heading<?php echo $i ?>">
<h5 class="mb-0">
<button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapse<?php echo $i ?>" aria-expanded="true" aria-controls="collapse<?php echo $i ?>">
Report #1: 8/6/2018
</button>
</h5>
</div>
<div id="collapse<?php echo $i ?>" class="collapse" aria-labelledby="heading<?php echo $i ?>" data-parent="#accordionExample">
<div style="text-align: center;" class="card-body">
<h3 style="float: left;"> Rating-Pull: </h3>
<?php
$i++;
echo $key;
echo "$value";
?>
</div>
</div>
</div>
<?php
}
}
?>
</div>
Which is great because it does what I thought I wanted it to do , which is this:
Display Output
What's not so great is now I realize that multiple reports are going to be in one accordion "folder" which is where the reportid row comes into play. So lets say I run my program and two (different) reports run on it but I want it in the say "folder" on the webpage. Both of these get labeled with a reportid of 1.
So what I want to do is loop through reports and then if they have the same ID group them together in that folder and iterate through the whole table like that. So that's the part where I have attempted to do so with a nested loop and SELECT 'report' FROM 'automation-reports' WHERE 'reportid' = '$i' ; and I just ended up getting the first element. Could somebody give me a hand with this and a good explanation so I can understand and learn what's happening?
Thank you!
EDIT:
Maybe a visual would be better?
VISUAL
I think GROUP BY and GROUP_CONCAT are what you are looking for.
SELECT `reportid`, GROUP_CONCAT(`report` SEPARATOR '') as report
FROM `automation-reports`
GROUP BY `reportid`
shoud do the job.
SELECT *
FROM `automation-reports`
GROUP BY `reportid`
will give you one row for each id ie.
id 1,
id 2,
id 3
Or do you want to display each row like so?
id 1, id 1,
id 2,
id 3, id 3,
id 4
if so here is a possible example of combining the reports in a loop first
$sql = "SELECT reportid, GROUP_CONCAT(report SEPARATOR ',') as reports FROM `automation-reports` GROUP BY `reportid`;";
while($row = mysqli_fetch_assoc($query)) {
echo "<div id='{$row['reportid']}'>";
echo $row['reports'];
echo "</div>";
}
I know this isn't the HTML you're after but you should be able to place your HTML in this code
I am displaying information in a card-based layout, but if any title in the card is longer than 41 characters, it doesn't fit into the card.
I thought about using [wordwrap], but all that would do is cause the one specific title to wrap and change the height of that card, affecting the layout of the other cards.
This is the PHP I use to print my results for pagination purposes, so it only prints 9 things at a time.
<div class="row">
<?php
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p><? echo $row["title"]; ?></p>
<p>Category: <? echo $row["category"]; ?></p>
</div>
</div>
</div>
<?php
};
?>
</div>
How would I go about line breaking every title if even one title is detected as longer than 41 characters?
EDIT: This is the solution I created:
$titlebreak = $row["title"];
if (strlen($titlebreak) >= 40)
{
$titlebreak2 = wordwrap($titlebreak, 39, "</p><p>");
}
else
{
$titlebreak2 = $row["title"] . "<p> </p>\n";
}
I've included 3 possible solutions below - adding manual line breaks as you asked about in your question; a basic but unsatisfactory CSS option and a jQuery solution which in this case is the one I would suggest as the most flexible.
Although a CSS-only solution is usually the preferred way of fixing a layout issue, when it comes to equal heights of elements there isn't a clear-cut way to do it and often a jQuery solution like the one below is required.
Manual line-break - as requested in your question
Instead of doing an additional SQL query as mentioned, you can easily do it in the PHP in 2 different ways:
(a) loop through the rows before displaying them to calculate the title lengths, then loop again to display with/without the line break
or
(b) if you really down't want to loop twice, you could include the line break regardless of length as you loop once, but also calculate the line length in that loop. Then hide the line break using CSS if its not required
(a) 2 loops: Calculate length to determine whether to add the line break or not:
<?php
$maxchars = 41;
$cards = array();
$bLongTitle = false;
while ($row = mysql_fetch_assoc($rs_result)) {
// save detaisl to $cards array
$cards[$row["title"]] = $row["category"];
// check title lengths until we find one over 41 - no need to check any more after that
if (!$bLongTitle && strlen($row["title"])) > $maxchars)
$bLongTitle = true;
}
?>
<div class="row">
<?php
foreach ($cards as $title => $category) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p><?
// if there were any long title, wrap them all text
if ($bLongTitle)
$title = wordwrap($title, $maxchars, "<br />\n");
echo $title;
?></p>
<p>Category: <? echo $category; ?></p>
</div>
</div>
</div>
<?php
}
?>
</div>
(b) 1 loop: Always display line break, and hide it if not required
<div class="row">
<?php
$bLongTitle = false;
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p class="cardTitle"><? echo wordwrap($title, $maxchars, "<br />\n"); ?></p>
<p>Category: <? echo $row["category"]; ?></p>
<?php
// check title lengths until we find one over 41 - no need to check any more after that
if (!$bLongTitle && strlen($row["title"])) > $maxchars)
$bLongTitle = true;
?>
</div>
</div>
</div>
<?php
};
?>
</div>
<?php if ($bLongTitle) {?>
<script>.cardTitle br {display:none;}</script>
<?php } ?>
CSS-only solution - not viable for the OP?
Because the titles are aren't direct siblings, the only way would be to fix the height of all title. This isn't a desirable solution, as I'm sure titles can vary a lot in length so its impossible to pick a "default" height to suit every possibility, and even that's complicated by the responsive width of the columns potentially changing the heights dynamically.
But for the sake of completeness:
add a class (e.g. .cardTitle) to the title in your loop
identify suitable heights for the title with and without a line break, and set these in your CSS
add the corresponding class (e.g. wrapTitle) to your <p> if any title is too long in a loop (similar to adding a line break above)
CSS
p.cardTitle { height:20px; } /* example of the default height for title */
p.cardTitle.wraptitle { height:40px; } /* example of height for wrapped title */
PHP (after looping through SQL rows to fill $cards array as option (a) above)
<?php
foreach ($cards as $title => $category) {
?>
<div class="col xl4 l4 m12 s12">
<div class="card z-depth-5">
<div class="card-content">
<p class="cardTitle <?php if ($bLongTitle) echo "wrapTitle"; ?>"><? echo $title; ?></p>
<p>Category: <? echo $row["category"]; ?></p>
</div>
</div>
</div>
<?php
};
?>
jQuery
You could use jQuery to loop through all elements to calculate the heights, and set them all to the tallest.
You could write the code yourself (see How to make all div columns of the same height automatically with jQuery or Setting equal heights for div's with jQuery but there is a library available to do this for you: matchHeight
Using the library, all you need to do is include it on your page and call it like this (assuming you've added the class cardTitle to the <p> that holds your title)
jQuery(document).ready(function() {
$(".cardTitle").matchHeight();
});
I would perform a new SQL query first to see if any results have a title length of greater than 41 characters:
select count(*) from table where length(title)>41
And then set the result of this query to be a variable, e.g. $has41
You can then use an if statement within your loop ...
if($has41) {
// do something with $row['title']
} else {
// do something else with $row['title']
}
I have a profile that shows profiles in a list. as shown in the image below.
users table
id | email | full_name | job_title | bio | profile_photo
images table
image_id | id | artist_img
CODE
<?php
$db = dbconnect();
$stmt = $db->prepare('SELECT
users.email,
users.full_name,
users.job_title,
users.bio,
users.profile_photo,
images.id,
images.artist_img
FROM users
INNER JOIN images ON users.id=images.id GROUP BY images.id');
$stmt->execute();
$result = $stmt->get_result();
while (($row = mysqli_fetch_assoc($result)) != false) {
$id = $row['id'];
$full_name = $row['full_name'];
$email = $row['email'];
$job_title = $row['job_title'];
$bio = $row['bio'];
$ProfilePhoto = $row['profile_photo'];
$artist_img = $row['artist_img'];
if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
$image = "$ProfilePhoto";
} else {
$image = "avatar.jpg";
}
echo "<div class='container team-wrap'>
<div class='row'>
<div class='col-md-6'>
<img class='img-responsive' src='artist/$image'>
</div>
<div class=\"col-md-6\">
<strong>$full_name<br>$job_title</strong>
<br>
<p>$bio</p>
<a href='mailto:$email' class='btn btn-info'>Contact Me</a>
</div>
</div>
</div>
<div class=\"container space team-wrap\">
<div class=\"row\">
<div class=\"col-lg-12\">
<div id=\"gallery-slider\" class=\"slider responsive\">
<div>";
echo"
<img src=\"gallery/$artist_img\" alt=\"\"></a>";
echo "</div>
</div>
<hr>
</div>
</div>
</div>";
}
?>
Problem area
echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>";
The issue I am having is that it repeats the profile for each image if the user has 5 images it will add 5 profiles 1 for each img.
and does not show the other users profile at all. show how it shows up is look at the image for example its got 4 images under profile 1 and it shows there profile pic.. well it repats all that info for each image I want the pics that have the same id as the user to show up as a slider like below..
and it also refuses to show the other profiles of other users.
yes because it is not seeing the variable because you echo it as text
echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>";
should be
$r=0;
foreach ($images as $image.id){
[$artist_img=$images[$r];
echo "<img src=\"gallery/".$artist_img."\" alt=\"\"></a>";
$r++;
}
//
i don't like to echo to much html because very easy to make a mistake that way i prefer it is to stay in html and just echo my variable like the but that is just me
<html>
<body>
<a><img src="gallery/<? php echo $artist_img; ?>" alt=""></a>
</body>
</html>
do you see here you fetch the variable for the picture as single row
$artist_img = $row['artist_img'];
you need to make it a array because it is a array you must remember using inner generates a number of pieces of data, maybe for you it will be better to run 2 query second query loads the images.inner join is useful for some people but complex and dont really give any advantage because still searches the whole tables twice to get the results
something like this might work for for you
$artist_img2=array();
//because there are more then one piece of data in it
$artist_img2 = $row['images'];
//then you need to do another loop to put the data in variables or echo them out
// in the loop
//note the row refers to id and not image.id because in the inner array the key will be id
$artist_img3=row2['id'];
echo "<img src=\"gallery/".$artist_img3."\" alt=\"\"></a>";
//end loop
I'm really struggling with something and wondered if anyone could spare a few moments to have a look at this code block.
The original line looked like this:
$home_collectionsx=get_home_page_promoted_collections();
This brought back all the promoted to homepage items and displayed them on the homepage. I however simply want to pull 1 item in using the same code and an array function, the id is 5 for this purpose so I thought adding =array(5) or (array (5)) would work - but it doesn't.
I'm hoping it's something simple, or something that I have missed or not written correctly.
<?php
if(!hook("EditorsPick")):
/* ------------ Collections promoted to the home page ------------------- */
$home_collectionsx=get_home_page_promoted_collections (array(5));
foreach ($home_collectionsx as $home_collectionx)
{
?>
<div class="EditorsPick">
<div class="HomePanel"><div class="HomePanelINtopEditors">
<div class="HomePanelINtopHeader">Editors Pick</div>
<div class="HomePanelINtopText">This is the editors pick of Asset Space...</div>
<div class="EditorsPicImage"><div style="padding-top:<?php echo floor((155-$home_collectionx["thumb_height"])/2) ?>px; margin-top: -24px; margin-bottom: -15px;">
<a href="<?php echo $baseurl_short?>pages/search.php?search=!collection<?php echo $home_collectionx["ref"] ?>" onClick="return CentralSpaceLoad(this,true);"><img class="ImageBorder" src="<?php echo get_resource_path($home_collectionx["home_page_image"],false,"thm",false) ?>" width="<?php echo $home_collectionx["thumb_width"] ?>" height="<?php echo $home_collectionx["thumb_height"] ?>" /></div>
</div></div>
</div>
</div>
</div>
<?php
}
endif; # end hook homefeaturedcol
?>
This is the function to the DB itself that the above code is connecting to…
function get_home_page_promoted_collections()
{
return sql_query("select collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width from collection left outer join resource on collection.home_page_image=resource.ref where collection.public=1 and collection.home_page_publish=1 order by collection.ref desc");
}
Any help would be hugely appreciated :-)
Many many thanks
Rich
That function doesn't take a parameter: get_home_page_promoted_collections()
You want something like:
$home_collectionsx=get_home_page_promoted_collections(5);
And:
function get_home_page_promoted_collections($id=null)
{
$filterClause = '';
if(!is_null($id))
{
//to only return this id
$filterClause = ' AND collection.ref = '.intval($id);
//to get all but that id
$filterClause = ' AND collection.ref != '.intval($id);
}
return sql_query("SELECT collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width FROM collection LEFT OUTER JOIN resource on collection.home_page_image=resource.ref WHERE collection.public=1 AND collection.home_page_publish=1".$filterClause." ORDER BY collection.ref DESC");
}