Need ideas in proper html rendering with php - php

I have constructed an SQL query to lay out some info on a web page. The query works well. The problem is with each iteration of while loop a CSS class needs to be auto-incremented by 1.
<div class="related-item item1">
'item1' should become 'item2' in the next iteration and so on. Can you give me something ideas how to do it?
<?php
//Construct the SQL query code
$rel = "SELECT entries.*, images.name
FROM entries, images
WHERE entries.id = blog_id
ORDER BY dateposted DESC
LIMIT 0, 3;";
//Send the query to the MySQL server
$result = mysql_query($rel);
//Pull the row as an associative array
while ($row = mysql_fetch_assoc($result)) {
echo '<div class="related-item item1">
<div class="thumbnail-wrapper">';
echo '<img src="./images/' . $row['name'] . '" alt="How SHAPE Reader Caitlin Flora Lost 182 Pounds"/></div>
<h4 class="related-article-title">
' . $row['subject'] . '
</h4>
</div>';
} //End of while loop
?>

Just keep a counter variable going
$cnt = 1;
while(fetch from db) {
echo "<a class='foo{$cnt}'>click me</a>";
$cnt++;
}
which produces
<a class='foo1'>click me</a>
<a class='foo2'>click me</a>
<a class='foo3'>click me</a>
etc...
but generally this sort of thing is NOT necessary for CSS. You'd have to create a css rule for EVERY one of those <a> elements being created, which gets incredibly ugly and repetitive. There is nth-child support in CSS, so you can write rules which "modify" themselves based on which child an element is (1st, 2nd, ... Nth).

Add an integer and increment it...
<?php
//Construct the SQL query code
$rel = "SELECT entries.*, images.name
FROM entries, images
WHERE entries.id = blog_id
ORDER BY dateposted DESC
LIMIT 0, 3;";
//Send the query to the MySQL server
$result = mysql_query($rel);
$i = 1;
//Pull the row as an associative array
while ($row = mysql_fetch_assoc($result)) {
echo '<div class="related-item item{$i}">
<div class="thumbnail-wrapper">';
echo '<img src="./images/' . $row['name'] . '" alt="How SHAPE Reader Caitlin Flora Lost 182 Pounds"/></div>
<h4 class="related-article-title">
' . $row['subject'] . '
</h4>
</div>';
$i++;
} //End of while loop
?>

Related

How to change the button text in the SQL in a PHP code?

The following is my code:
<?php $result = mysql_query("SELECT * FROM pm_categories WHERE parent_id = 512 ORDER BY tag desc"); ?>
<?php while($row = mysql_fetch_assoc($result)) { ?>
<a class="button button-blue plus20 category" href="https://www.parvizshahbazi.com/ganj_videos/app/listvideos.php?range=<?php echo $row['tag']; ?>"><?php echo str_replace("-", " - ", $row['tag']); ?></a>
<?php } ?>
This is the result:
The buttons have numbers 1-100,101-200 and so on
I was wondering how I can have 801-1000 instead of 801-900. I know that the digits are created from the while loop. Is there anyway to manually create the 801-1000 button?
I tried to manipulate the chart numbers in SQL, yet if 801-900 is manually changed to 801-1000, it gives an error. Because they are in the category of 801-900. What I want is just 801-1000 on the button for 801-1000.
Here is my SQL 'pm_config':
pm_config list
In sum, this is what I want in the end (it is photo-shopped):
What I desire to have in the end
You could, in your 'while' loop, create a specific rule, like this:
while($row = mysql_fetch_assoc($result))
{
// if your loop is treating the 801-900 tag, then just change it to 801-1000
if($row['tag'] == '801-900') { $row['tag'] = '801-1000'; }
echo '<a class="button button-blue plus20 category" href="https://www.parvizshahbazi.com/ganj_videos/app/listvideos.php?range=' . $row['tag'] . '">' . str_replace("-", " - ", $row['tag']) . '</a>';
}

Is it possible to wrap looping PHP output in tags?

everyone. I'm having a bit of a PHP conundrum here and I couldn't find a good answer that already existed. You see, I'm working on a project where I have to take a classmate's discography website and revamp it with PHP, to where, instead of having the album covers and tracklists hard-coded in, it would query the database for them. My problem is that I have to keep the general style of his site intact, and I'm having trouble doing that. Basically his styles depend on having the album cover, name, and tracklists in div tags, and the style he's got in place is achieved through both Bootstrap and his own, custom CSS stylesheet.
Before I start to ramble, my question is: is there any way to wrap looping output in HTML tags? I need to get the album cover, album name, and tracklists in a div tag, but only the tracklists loop. Here is the code I have in place to query the database:
<?php
require ('mysqli_connect.php');
// Connect to database server
mysql_connect("localhost", "admin", "instructor") or die(mysql_error());
// Select database
mysql_select_db("phprediscography") or die(mysql_error());
// SQL query
$q = "SELECT DISTINCT albums.albumname, albums.albumID, albums.coverart
FROM albums
JOIN tracks
ON albums.albumID=tracks.albumID"; //select UNIQUE results from database
$t = "SELECT trackname FROM tracks WHERE albumID = 1";
$b = "SELECT trackname FROM tracks WHERE albumID = 2";
$n = "SELECT trackname FROM tracks WHERE albumID = 3";
$r = "SELECT trackname FROM tracks WHERE albumID = 4";
$result = mysqli_query($dbcon, $q);
$result1 = mysqli_query($dbcon, $t);
$result2 = mysqli_query($dbcon, $b);
$result3 = mysqli_query($dbcon, $n);
$result4 = mysqli_query($dbcon, $r);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { //loop through database to get each album
echo '<img class="img-responsive" src=' . $row['coverart'] . '>' . '<br />';
echo '<h2>' . $row['albumname'] . "</h2><br />";
if ($row['albumID'] == 1) {
foreach($result1 as $row1) { //loop through tracks and output to page
echo '<p>' . $row1['trackname'] . '</p>';
}
}
if ($row['albumID'] == 2) {
foreach($result2 as $row2) { //loop through tracks and output to page
echo '<p>' . $row2['trackname'] . '</p>';
}
}
if ($row['albumID'] == 3) {
foreach($result3 as $row3) { //loop through tracks and output to page
echo '<p>' . $row3['trackname'] . '</p>';
}
}
if ($row['albumID'] == 4) {
foreach($result4 as $row4) { //loop through tracks and output to page
echo '<p>' . $row4['trackname'] . '</p>';
}
}
}
// Close the database connection
mysql_close();
?>
If I need to post anything else, let me know, this is my first-ever question so I'm just kind of feeling it out.
By doing your $t = "SELECT trackname FROM tracks WHERE albumID = #"; and if($row['albumID']==#) you are essentially still hardcoding similar to your friend. Just do 1 query, where you join all the tracks. Then when looping, group by the albumname -
<?php
require('mysqli_connect.php');
// SQL query
$q = "SELECT albums.albumname, albums.albumID, albums.coverart, tracks.trackname
FROM albums
JOIN tracks
ON albums.albumID=tracks.albumID";
$result = mysqli_query($dbcon, $q);
$current_albumID = ""; //create current albumID var to be used below.
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){//loop through database to get each album
if($row['albumID'] != $current_albumID){
echo '<img class="img-responsive" src='.$row['coverart'] . '>' . '<br />';
echo '<h2>' . $row['albumname'] . "</h2><br />";
$current_albumID = $row['albumID']; // set current albumID to this albumID
}
echo '<p>' . $row['trackname'] . '</p>';
}
?>
Try something like this instead: Get all the data you're after in your first query, then use php to process that into your output:
$q = "SELECT albums.albumname, albums.albumID, albums.coverart, tracks.trackname
FROM albums
JOIN tracks ON albums.albumID=tracks.albumID";
$result = mysqli_query($dbcon, $q);
$lastAlbumId = null;
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
if ($lastAlbumId != $row['albumID']) {
echo '<img class="img-responsive" src="'.htmlentities($row['coverart']).'"><br />';
echo '<h2>'.htmlentities($row['albumname']).'</h2><br />';
}
echo '<p>'.htmlentities($trackname).'</p>';
$lastAlbumId = $row['albumID'];
}
A few things to note:
I added use of htmlentities to escape the user data so malicious people can't type in HTML somewhere and have it appear on your site. This is something you should do almost everywhere you're displaying data from a database in a html site, except for very rare cases where you know what you're doing.
You probably don't need those <br /> tags - <h2> is a block level element so it'll force itself onto it's own line anyway (unless there's some silly CSS rules somewhere).
Also note - the above code is untested - typed straight into browser. There may be some syntax errors - let me know if you see any problem and I'll happily edit the answer. (or you can suggest an edit).

Deleting printed content in PHP mysql

I'm creating a site to upload content and I want the user to be able to delete content on their Edit page. The following code is what I have echoed after the user inputs their content
<div id="content_review">
<?php
$q_getRev = "SELECT * FROM REVIEWS ORDER BY RID DESC";
$r_getRev = mysql_query($q_getRev, $connection);
$n_getRev = mysql_num_rows($r_getRev);
for ($i = 0; $i < $n_getRev; $i++) {
echo '<img class="poster" src="'
. mysql_result($r_getRev, $i, 'PREVIEW_IMG') . ' " />';
echo '<div class="title">'
. mysql_result($r_getRev, $i, 'TITLE') . '</div><br/>';
echo '<div class="reviews">'
. mysql_result($r_getRev, $i, 'REVIEW') . '</div>';
echo '<div><img src=image/x.png id="delete">'
. mysql_result($_delRev, $i, 'REVIEW') . '</div>';
}
?>
I want the user to be able to delete content that was previously printed in PHP. I'm not entirely sure how to proceed to make the following line delete content from my REVIEW column from mysql (the following line appears as a delete button next to the already printed content)
echo '<div><img src=image/x.png id="delete">'
. mysql_result($_delRev, $i, 'REVIEW').'</div>';
That line should be:
echo '<div><img src="image/x.png"></div>';
Then you need to write a delete_content.php script that deletes the content whose RID is equal to $_GET['rid']. The PHP in that script should be something like:
$rid = intval($_GET['rid');
$delRev = "DELETE FROM REVIEWS WHERE RID = $rid";
Also notice that I removed the id="remove" attribute from the <img> tag. IDs have to be unique, but you were using the same ID for each item. I doubt you need an ID on the image tags, so I just removed it.

PHP "Placeholder" Results-Tabs

Building an app that queries database for optical images for patients. The results are sorted by date and organized into tabs. Each patient could have any number of tabs based on their history of doctor visits. My tabs are organized horizontally at the top of my page. When there is only one row worth of results tabs (no matter how many) the page looks fine. However, if the amount of tabs exceeds the amount that can be contained in the first row (about 10), a second row starts. This creates a gap between my results tabs and the div that holds their contents.
What I want to know is if it would be possible to create extra "placeholder" tabs that could fill this empty space and how I might go about doing that. Obviously every patient's results are different so I would need to make enough extra tabs to fill any sized gap.
CODE:
<ul class="tabs" id="results">
<?php
if(isset($_GET['id']) && $_GET['id'] != '')
{
$id = $_GET['id'];
$query = "select * from SHOT join VISIT on VISIT.ID=SHOT.VISIT_ID JOIN PATIENT ON SHOT.PATIENT_ID=PATIENT.ID where SHOT.PATIENT_ID=
" . $id . " order by VISIT.VISIT_DATE DESC;";
$res = mysql_query($query);
//Here we count the number of returned rows. If it returned nothing then it will store 0.
$count = mysql_num_rows($res);
$i = 0;
$j = 1;
if($count > 0)
{
$previous_vdate = '';
while($row = mysql_fetch_array($res))
{
$id = $row['ID'];
$vdate = $row['VISIT_DATE'];
$img = $row['IMG_FILENAME'];
$subdir = $row['SUBDIR'];
$subsubdir = $row['SUBSUBDIR'];
if ( strcmp("$vdate", "$previous_vdate") != 0)
{
$formatted_vdate = explode(" ", $vdate);
$formatted_vdate = explode("-", $formatted_vdate[0]);
$visit_date = $formatted_vdate[1] . "-" . $formatted_vdate[2] . "-" . $formatted_vdate[0];
if ($j == 1)
{
echo "<li class='active' rel='tab" . $j . "'>" . $visit_date . "</li>";
}
else
{
echo "<li rel='tab" . $j . "'>" . $visit_date . "</li>";
}
$previous_vdate = $vdate;
$j++;
}
$i++;
}
}
else
{
echo "<div id='no_result'>No result found !</div>";
}
echo "<li rel='tab" . $j . "'>All Dates Combined</li>";
}
else
{
echo "<p>Something's Broke</p>";
}
?>
All help is appreciated. If there's any other info I can add let me know... I'll be in front of the computer for the next several hours, checking/refreshing this page.
Generated HTML:
<ul id="results" class="tabs"><li class="active" rel="tab1">
04-23-2012
</li><li rel="tab2">
04-10-2012
</li><li rel="tab3">
03-05-2012
</li><li rel="tab4">
06-06-2011
</li><li rel="tab5">
12-14-2010
</li><li rel="tab6">
12-14-2009
</li><li rel="tab7">
12-15-2008
</li><li rel="tab8">
12-12-2007
</li><li rel="tab9">
02-07-2007
</li><li rel="tab10">
11-01-2006
</li><li rel="tab11">
11-03-2005
</li><li rel="tab12">
03-13-2002
</li><li rel="tab13">
All Dates Combined
</li><li>
Here is the screen shot: http://imgur.com/5lYpj5Y
If you are allowed to use Javascript I can recommend you to use auto-fit:
Demo:
http://pmbennett.net/demos/autofit-tabs.html (try it by changing the size of your browser window)
Source:
http://pmbennett.net/2010/03/26/auto-fit-tabs/
Disadvantage: Not a CSS-only solution
Personally I think a tab without function, just to fill the empty space, would confuse users. I think users would click it and nothing would happen. Maybe it is also easier to give the empty space a lighter (grey?) background.

PHP/MYSQL populate dropdown sorted by query results

What I'd like is to create a new unordered list for each new value in a mysql result column.
My query looks like this and i'm throwing it into a data array:
$connection = dbconnect();
$getusers = "SELECT users.usr_id, users.usr_firstname, users.usr_lastname, user_groups.group_name FROM users INNER JOIN user_groups ON users.usr_group = user_groups.group_id ORDER BY group_name ASC, users.usr_firstname ASC, users.usr_lastname ASC";
$result = mysql_query($getusers);
$data_array = array();
while($data = mysql_fetch_array($result)){
$data_array[] = $data;
}
Now I need to display that data so that each new user_group is an unordered list, and each row with that same group, comes up as a list item of that unordered list.
it's very similar to this question ( PHP/MySQL Query Results ), but i'm having trouble getting the closing ul's in the right place. Here's my code for outputting, though I know it's wrong because the li's aren't really children of the ul's.
$previousgroup = "";
foreach($data_array as $users){
if($users['group_name'] != $previousgroup){
echo "<ul class=\"group\">" . $users['group_name'] . "</ul>";
}
else{
echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
}
}
Is there a more efficient way of doing this? Thanks for your help.
You could change the while loop this way:
$data_array = array();
while($data = mysql_fetch_array($result)){
$data_array[$data['group_name']][] = $data;
}
and you would get an array that contains your group_name's as index of other arrays which contain your data. To create the list, you could do the following:
<ul>
<?php
foreach ($data_array as $temp_key => $temp_array)
{
?>
<li>
<?php echo $temp_key; ?>
<ul>
<?php
foreach ($temp_array as $datum)
{
echo ("<li>" . $datum['usr_firstname'] . "</li>";
}
?>
</ul>
</li>
<?php
}
?>
</ul>
Hope it works for you
if($users['group_name'] != $previousgroup){
echo "</ul><ul class=\"group\"><li>" . $users['group_name'] . "</li>";
}
echo "<li id=\"m" . $users['usr_id'] . "\"><h4>" . $users['usr_firstname'] . " " . $users['usr_lastname'] . ", " . $users['cred_name'] . "</h4></li>";
The code in your loop should look like this, and you need to open and close a <ul> from outside the loop.
echo '<ul>';
foreach () { // here is the loop }
echo '</ul>';
THis is doing it the "dirty" way with printing out as you go, at least.
The way I would do it would be to build a 2D array indexed first by group_name and then by user ID. It would mean making two passes at the data as a second loop would be needed to display the data, but generally speaking you're not going to feel the difference.

Categories