This is the function im using to show results in the index.php with the style.css correctly done with the li´s and the div´s all that i just want to show in every li the price or the service i want. EXAMPLE: ($_GET_ROW['ID'] = '1', $_GET_ROW['ID'] = '2').
function products() { $get = mysql_query('SELECT id, servico, preco FROM loja');
if (mysql_num_rows($get)==0) {
echo "Não existem produtos a serem mostrados!";
} else {
while ($get_row = mysql_fetch_assoc($get)) {
echo '<li class="l1 i1 column1">';
echo '<h2>'.**$get_row["servico"]**. '</h2>';
echo '<div class="basket">Adicionar</p></div>';
echo '<div class="preco"><em>Preco:</em><strong>'.number_format($get_row['preco'], 2).'</strong><span>EUR</span>';
echo '</div>';
echo '<li class="l2 i0 column0">';
echo '<h2>'**.$get_row["servico"].**'</h2>';
echo '<div class="basket">Adicionar</p></div>';
echo '<div class="preco"><em>Preco:</em><strong>'.number_format($get_row['preco'], 2).'</strong><span>EUR</span>';
echo '</div>';
My database:
-----------------------------------
id servico preco
-----------------------------------
1 Servico Normal 29.5
-----------------------------------
2 Servico Rapido 32.5
-----------------------------------
It seems like you need a while loop to iterate through the rows like so:
if (mysql_num_rows($get)==0) {
echo "Não existem produtos a serem mostrados!";
} else {
while($get_row = mysql_fetch_assoc($get)) {
**echo '<li class="l1 i1 column1">';
echo '<h2>'.$get_row["servico_normal"].'</h2>';
echo '<div class="basket">Adicionar</p></div>';
echo '<div class="preco"><em>Preco:</em <strong>'.number_format($get_row['preco'], 2).'</strong><span>EUR</span>';
echo '</div>';**
}
}
However, you should immediately stop using mysql_ functions as they are being deprecated and vulnerable to SQL injection; and use mysqli_ or PDO instead.
Update 1
mysql_ functions are officially deprecated.
The principle of my initial answer still remains. You have two rows in your database and you want to iterate through them.
while ($get_row = mysql_fetch_assoc($get)) {
$liClass = 'l' . $get_row[id];
echo '<li class="' . $liClass . ' i1 column1">';
echo '<h2>' . $get_row[servico] . '</h2>';
echo '<div class="basket"><p>Adicionar</p></div>';
echo '<div class="preco"><em>Preco:</em><strong>' . number_format($get_row['preco'], 2) . '</strong><span>EUR</span>';
echo '</div>';
}
For the first row, the output would be:
<li class="l1 i1 column1">
<h2>Servico Normal</h2>
<div class="basket"><p>Adicionar</p></div>
<div class="preco"><em>Preco:</em><strong>29.50</strong><span>EUR</span></div>
Related
I have built a list (moviesSeen) where people can save their seen movies.
Now I want to get the director for each movie from an API and then compare which movies have already been seen. The output of the movies works without problems, but I can't get the result from the database (moviesSeen) to match the movies of the director. How can I achieve this?
The goal of the script is to show all movies of a director and mark all the ones you have already seen.
function getCrewDetails() {
global $apiKey;
global $language;
$personID = htmlspecialchars($_GET['personID']);
$url = "https://api.themoviedb.org/3/person/" . $personID . "?api_key=" . $apiKey . "&" . $language . "";// path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$personPrivate = json_decode($data); // decode the JSON feed
echo '<div class="row">';
echo '<div class="col">';
echo '<img src="https://image.tmdb.org/t/p/w300/' . $personPrivate->profile_path . '" class="img-thumbnail">';
echo '</div>'; // end div col
echo '<div class="col-8">';
echo '<h1>' . $personPrivate->name . '</h1>';
if (!empty($personPrivate->biography)) {
echo $personPrivate->biography;
} else {
echo '<p>Verdammmt! Zu dieser Person liegen keine biographischen Details vor.</p>';
echo '<p>Dabei hätte sie es doch so verdient :(</p>';
}
echo '</div>'; // end div col
echo '</div>'; // end div row
// echo '<pre>';
// print_r($person);
$url = "https://api.themoviedb.org/3/person/" . $personID . "/movie_credits?api_key=" . $apiKey . "&" . $language . "";// path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$personCareer = json_decode($data); // decode the JSON feed
echo '<div class="container mt-4">';
echo "<h2>Filmographie</h2>";
echo '<table class="table table-striped table-sm">';
echo '<thead>';
echo '<tr>';
echo '<th scope="col">Film</th>';
echo '<th scope="col">Gesehen</th>';
echo '<th scope="col">Funktion</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
global $pdo;
$stmt = $pdo->prepare("SELECT * FROM movieSeen WHERE user_id = '" . $_SESSION['id'] . "'");
$stmt->execute();
foreach ($stmt as $row ) {
echo $row['movie_id'] . ', ';
} // output of the saved IDs in movieSeen
foreach ($personCareer->crew as $showCrewDetails) { //Output of the movies
echo '<tr>';
echo '<td>' . $showCrewDetails->title . ' ' . $showCrewDetails->id . ' (' . substr($showCrewDetails->release_date, 0, 4) . ')</td>';
echo '<td><span class="badge badge-danger">Movie Seen</span></td>';
echo '<td>' . $showCrewDetails->job . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
//echo '<pre>';
//var_dump($personCareer);
}
I know that I have to edit the last foreach loop, but I just can't get it to work :(
I am currently trying to create a class schedule which I pull from my Sql Server database with PHP and I am trying to get the layout output as well as the data as I am grouping the resources.
These groupings are nested such as:
-DAY
--TIME
---CLASS
----STUDENTS
And should output like this:
However, I am getting this:
My current output is working, however, it is only on the first loop, then everything goes haywire. I am assuming there is an erroneous </div> tag somewhere in my code yet I cannot for the life of me find it.
My php code is a function that is delcare as such:
<div class="mainScheduleWrapper">
<?php daySchedule(); ?>
</div>
My php code is as such:
function daySchedule() {
global $conn;
$dayScheduleQuery = 'SET DATEFIRST 1
SELECT [DAY].[DAY] AS [DAY], CLASS.CLASSTIME AS CLASSTIME, CLASSLEVEL.CLASSLEVEL AS CLASSLEVEL, CLASS.MAXSTUDENT AS MAXSTUDENT, INSTRUCTOR.FIRSTNAME AS INSTRUCTOR, STUDENT.FIRSTNAME AS STUDENTFIRST, STUDENT.SURNAME AS STUDENTLAST, STUDENT.DOB AS STUDENTDOB
FROM STUDENT JOIN BOOKING ON STUDENT.ID = BOOKING.STUDENTID JOIN CLASS ON CLASS.ID = BOOKING.CLASSID JOIN CLASSLEVEL ON CLASS.CLASSLEVELID = CLASSLEVEL.ID JOIN [DAY] ON CLASS.CLASSDAY = [DAY].ID JOIN INSTRUCTOR ON CLASS.INSTRUCTORID = INSTRUCTOR.ID
WHERE [DAY].ID = (DATEPART(dw, GETUTCDATE() AT TIME ZONE \'AUS Eastern Standard Time\'))
ORDER BY CLASS.CLASSTIME ASC, INSTRUCTOR.FIRSTNAME ASC, CLASSLEVEL.CLASSLEVEL ASC';
// COUNTERS
$t = 0;
$i = 0;
//VARIABLES FOR DAY SCHEDULE
$classDay = NULL;
$classTime = NULL;
$classInstructor = NULL;
$closeClass = false;
$closeAll = false;
$queryConnector = $conn->query($dayScheduleQuery);
foreach ($queryConnector as $schedule) {
// CLASS DAY HEADER
if ($classDay != $schedule['DAY']) {
echo '<div class="grid-1">';
echo '<h1>' . $schedule['DAY'] . '</h1>';
echo '</div><!-- Day closed! -->';
$classDay = $schedule['DAY'];
}
// CLASS TIME HEADER
if ($classTime != $schedule['CLASSTIME']) {
if($classTime != $schedule['CLASSTIME'] && $t > 0) {
$closeAll = true;
goto closeAll;
}
echo '<div class="grid-12-noGutter scheduleContainer">'; //NON-CLOSED
echo '<h1>' . 'T = ' . $t . '</h1>';
echo '<div class="grid-middle-center col scheduleTimeTab">';
// FIX 3 DIGIT MILITARY TIME
if (strlen($schedule['CLASSTIME']) < 4) {
$classScheduleTime = '0' . $schedule['CLASSTIME'];
} else {
$classScheduleTime = $schedule['CLASSTIME'];
}
echo '<p>' . date('g:i A', strtotime($classScheduleTime)) . '</p>';
echo '</div>'; //CLOSE TIME TAB
echo '<div class="innerSchedule">'; // NON-CLOSED
$classTime = $schedule['CLASSTIME'];
$t += 100;
}
// INSTRUCTOR HEADER
if ($classInstructor != $schedule['INSTRUCTOR']) {
if ($classInstructor != $schedule['INSTRUCTOR'] && $i > 0) {
$closeClass = true;
goto closeClassWrapper;
}
echo '<div class="classWrapper">';
echo '<h1>' . 'I =' . $i . 'T = ' . $t . '</h1>';
echo '<div class="grid-3-middle classHeader">';
echo '<div class="col classHeaderCell' . classLevelColour($schedule['CLASSLEVEL']) . '">' . $schedule['CLASSLEVEL'] . '</div>';
echo '<div class="col classHeaderCell">' . $schedule['INSTRUCTOR'] . '</div>';
echo '<div class="col classHeaderCell">Max' . ' ' . $schedule['MAXSTUDENT'] . '</div>';
echo '</div>';
echo '<div class="grid-4-middle" id="studentHeaders">';
echo '<div class="col"><h6>Student Name</h6></div>';
echo '<div class="col"><h6>Student Birthday</h6></div>';
echo '<div class="col"><h6>Class Level</h6></div>';
echo '<div class="col"><h6>Attendance</h6></div>';
echo '</div>';
$classInstructor = $schedule['INSTRUCTOR'];
$i += 100;
}
echo '<div class="grid-4 studentRow">';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['STUDENTFIRST'] . ' ' . $schedule['STUDENTLAST'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['STUDENTDOB'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['CLASSLEVEL'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">--</span>';
echo '</div>';
echo '</div>';
// GOTO TAGS
closeClassWrapper: {
if ($closeClass === true) {
echo '</div>';
$closeClass = false;
$i = 0;
}
}
closeAll: {
if ($closeAll === true) {
echo '</div>';
echo '</div>';
echo '</div>';
$closeAll = false;
$t = 0;
$i = 0;
}
}
}
}
Any help would be greatly appreciated - even if it's to tell me I'm going about it the completely wrong way.
Kindest Regards
Michael Z
I wouldn't say you're going about it the completely wrong way, but a few red flags jumped out at me in your code.
The use of goto jumping is bad practice. It butchers your program flow and forces you to segregate tasks that shouldn't be kept apart. You marked the sections of code "// NON CLOSED" when there was a </div> missing, is there any purpose for that? How do you know the goto sections are reliable?
When you echo something like <div class="col">, without escaping the double-quotes (as in \" for every " character), it can be problematic. Your code can get mangled or misinterpreted, both on the PHP end or on the HTML end.
Like others have said, the use of PHP may be overkill here. Besides just sending the JSON, the rest could be handled with JavaScript.
I have this JOIN, that gets me more than on result:
at.tipo AS atividade,
at.data AS datacadastro,
at.user AS usercadastro,
LEFT JOIN atividades at
ON (at.produto = '$produto')
AND (at.tipo = 'cadastra' OR at.tipo = 'revisado')
And I have: if string = 'cadastra' shows something, if string = 'revisado' shows something else.
<?php
if ($row['atividade'] == 'cadastra') {
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
}
?>
<?php
if ($row['atividade'] == 'revisado') {
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
}
?>
The problem is, it's showing just the first result, and not all.
How can I display all results?
EDIT:
I have this JOIN inside a big query, that give me other important rows.
But I just want one result for this principal query (WHERE = $produto), and want more results from my JOIN table.
$rows = $result->num_rows;
for ($j = 0; $j < $rows; ++$j) {
$result -> data_seek($j);
$row = $result->fetch_array (MYSQLI_ASSOC);
?>
My result should be this, but JOIN shows just row 'cadastra'.
Use $result->fetch_array (MYSQLI_ASSOC) inside a while loop
while ($row = $result->fetch_array (MYSQLI_ASSOC)) {
if ($row['atividade'] == 'cadastra')
{
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
} ?>
<?php if ($row['atividade'] == 'revisado')
{
echo '<div id="user-img">';
echo '<img src="http://'.$row['imguser'].'"/></div>';
echo '<div id="user-cadastro" class="greytxt">';
echo 'Cadastrado por <br>' .$row['usercadastro']. '<br>em ';
echo $row['datacadastro']. '</div>';
}
}
I'm trying to list diary events in PHP from a MySQL database, but grouped by days. I'll explain.
This is a screenshot of what I have so far:
As you can see there are two diary events for the 23rd October 2012 and is showing two calendar icons/representations/whatever's. I actually want it to show one calendar icon on the left but list all of that days events on the right, until the next day - as seen in my a̶r̶t̶i̶s̶t̶s̶ idiots impression below:
This is the code I have just written, could somebody please point me in the right direction:
$SQL = "SELECT entry_id, entry_title, entry_body, entry_date, entry_day, entry_month, entry_year ";
$SQL .= "FROM pages_diary WHERE entry_month = :this_month AND entry_year = :this_year ";
$SQL .= "ORDER BY entry_date DESC;";
// PDO stuff
if ($STH->rowCount() > 0) {
while($row = $STH->fetch()):
$this_db_month_word = mktime(0, 0, 0, $row['entry_month']);
$this_db_month_word = strftime("%b", $this_db_month_word);
echo '<div class="diary_events_item" id="diary_events_item_'.$row['entry_id'].'">';
echo '<div class="diary_events_item_left">';
echo '<div class="calendar_wrap">';
echo '<div class="calendar_wrap_top">';
echo $this_db_month_word;
echo '</div>';
echo '<div class="calendar_wrap_bottom">';
echo str_pad($row['entry_day'],2,'0',STR_PAD_LEFT);;
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="diary_events_item_right">';
echo '<strong>'.htmlspecialchars($row['entry_title']).'</strong><br>';
echo '<p>'.htmlspecialchars($row['entry_body']).'</p>';
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
endwhile;
} else {
echo '<p>There are no diary entries logged for <strong>'.$this_month_word.' '.$this_year.'</strong>.</p>';
}
Not looking for exact code (although that would be spiffing), just an explanation would do, I'm sure I can work it out from that.
THE FIX
At the end of the WHILE loop, I added:
$last_db_day = $row['entry_day'];
And then wrapped the calendar item in:
if ($last_db_day != $row['entry_day']) {
echo '<div class="calendar_wrap_top">';
echo $this_db_month_word;
echo '</div>';
echo '<div class="calendar_wrap_bottom">';
echo str_pad($row['entry_day'],2,'0',STR_PAD_LEFT);;
echo '</div>';
}
As you loop over your resultset from the database, keep track of the last entry_date that you've seen and only output a new calendar icon if the current record is for a different date.
I usually do this as follows:
if ($STH->execute()) {
// output headers
$row = $STH->fetch();
while ($row) {
$current_date = $row['entry_date'];
// output $current_date initialisation
do {
// output event $row
} while ($row = $STH->fetch() and $row['entry_date'] == $current_date);
// output $current_date termination
}
// output footers
}
Although I would go the other way around and embed the PHP within the HTML rather then the other way around, here is what I would do using your code:
$SQL = "SELECT entry_id, entry_title, entry_body, entry_date, entry_day, entry_month, entry_year ";
$SQL .= "FROM pages_diary WHERE entry_month = :this_month AND entry_year = :this_year ";
$SQL .= "ORDER BY entry_date DESC;";
// PDO stuff
if ($STH->rowCount() > 0) {
$loopDay = '';
while($row = $STH->fetch()):
if ($row['entry_day'] != $loopDay) {
$loopDay = $row['entry_day'];
$changed = true;
}
$this_db_month_word = mktime(0, 0, 0, $row['entry_month']);
$this_db_month_word = strftime("%b", $this_db_month_word);
echo '<div class="diary_events_item" id="diary_events_item_'.$row['entry_id'].'">';
echo '<div class="diary_events_item_left">';
if ($changed) {
echo '<div class="calendar_wrap">';
echo '<div class="calendar_wrap_top">';
echo $this_db_month_word;
echo '</div>';
echo '<div class="calendar_wrap_bottom">';
echo str_pad($row['entry_day'],2,'0',STR_PAD_LEFT);;
echo '</div>';
echo '</div>';
} else {
echo ' ';
}
echo '</div>';
echo '<div class="diary_events_item_right">';
echo '<strong>'.htmlspecialchars($row['entry_title']).'</strong><br>';
echo '<p>'.htmlspecialchars($row['entry_body']).'</p>';
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
$changed = false;
endwhile;
} else {
echo '<p>There are no diary entries logged for <strong>'.$this_month_word.' '.$this_year.'</strong>.</p>';
}
I'm trying to display the first row in one color and the second row in another color but my code displays the result twice in both colors for example lets say I have 5 results my code will double the results by displaying 10 results. How can I fix this problem?
Here is the php code.
while ($row = mysqli_fetch_assoc($dbc)) {
//first row
echo '<h3 class="title">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
//second row
echo '<h3 class="title-2">' . $row['title'] .'</h3>';
echo '<div class="summary-2">' . substr($row['content'],0,255) . '</div>';
}
You need to change the class on each row:
$count = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if( $count % 2 == 0 ) {
$classMod = '';
} else {
$classMod = '-2';
}
//first row
echo '<h3 class="title' . $classMod . '">' . $row['title'] .'</h3>';
echo '<div class="summary' . $classMod . '">' . substr($row['content'],0,255) . '</div>';
$count++;
}
your code should be like this
CSS
.odd { background: #CCC }
.event { background: #666 }
PHP
$c = true;
while ($row = mysqli_fetch_assoc($dbc)) {
$style = (($c = !$c)?' odd':' even');
echo '<h3 class="title '.$style.'">' . $row['title'] .'</h3>';
echo '<div class="summary '.$style.'">' .substr($row['content'],0,255) . '</div>';
}
Here's a solution with minimal repetition:
$count = 0;
while (($row = mysqli_fetch_assoc($dbc)) && ++$count) {
printf(
'<h3 class="title%1$s">%2$s</h3>'
. '<div class="summary%1$s">%3$s</div>'
, $count % 2 ? "" : "-2"
, $row['title'] // might want to use htmlentities() here...
, substr($row['content'], 0, 255) // and here...
);
}
$i = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
$color =$i % 2;
echo '<h3 class="title-' .$color . '">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
$i++;
}
Your code just displays every result twice. Use a conditional (e.g. an integer or a boolean) to switch between rows (like: if true, then green; if false, then red).
For a boolean you could change the current value like so:
bool = !bool;
Couple of extra points:
You don't (normally) need so many classes. If you have <div class="stripe"> as your container, you can target the items with e.g. .stripe h3 in CSS.
If you target the odd and even items in CSS with .stripe h3, you can then overwrite just the odd items.
In a perfect world, you should keep presentation in the CSS. All browsers but IE7 and below support div:odd to target any odd child of a parent. This may require changing the structure of your HTML slightly. For IE7 and below, I'd add classes with JavaScript instead of PHP. When IE7 is no more then you can just remove the JS.
By the way, you could do this code too:
while ($row = mysqli_fetch_assoc($dbc)) {
echo '<h3 class="title">' . $row['title'] .'</h3>';
echo '<div class="summary">' . substr($row['content'],0,255) . '</div>';
if($row = mysqli_fetch_assoc($dbc)){
echo '<h3 class="title-2">' . $row['title'] .'</h3>';
echo '<div class="summary-2">' . substr($row['content'],0,255) . '</div>';
}
}
IMO, there is no excuse for not delegating this kind of non-critical, presentation layer decoration to client side code.
Just use a library like jQuery and access the odd and even rows like so:
<script>
$(document).ready(function()
{
//for your table rows
$("tr:even").css("background-color", "#F4F4F0");
$("tr:odd").css("background-color", "#EFF1F2");
});
</script>
You'll have us generating font tags next.