I cant pass variables between loops using PHP and SQL? - php

In the loops below, PHP doesn't seem to pass the outputted string back out to the loop that contains it for further manipulation, then making the output from this loop nothing at all.
Please help me understand why this is...
<?php
$finalTablePrint = '<html><body style="font-family:Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:40px;">
LTU Activity Report<br/>
<span style="font-size:18px;"> Date Created: ' . date("l jS \of F Y") . '<br/><br/>
<table width=100% style="background-color:white; font-size:12px; border-collapse:collapse;" >
<tr style="font-size:12px; font-weight:600; background-color:#d2dbdf; height:35px;">
<td>Activity</td><td>Department</td><td>Hours Spent</td><td>Month</td>
</tr>';
while ($row = mysql_fetch_assoc($result)) {
$finalTablePrint .= '<tr><td colspan=4>' . $row['activity'] . '</td></tr>';
while ($row_1 = mysql_fetch_assoc($result)) {
if ($row_1['activity'] == $row['activity'] && $row_1['department'] == $row['department']) {
$finalTablePrint .= '<tr style="height:30px;"><td colspan=3>' . $row['department'] . '</td>' . '<td>' . $row['hours'] . '</td><td>' . $row['month'] . '</td></tr>';
}
}
}
echo $finalTablePrint .='</table><script type="text/javascript">javascript:window.print(); setTimeout(\'window.location="../../admin/index.php"\',"1000");</script></body></html>';

Note that when you set off your second loop, you are calling the same SQL statement as the original one before it
while ($row = mysql_fetch_assoc($result)) { //1st while
while ($row_1 = mysql_fetch_assoc($result)) { //2nd while
which in turn means that this if statement will allways be true....
if ($row_1['activity'] == $row['activity'] && $row_1['department'] == $row['department']) {
and this will allways echo:
$finalTablePrint .= '<tr style="height:30px;"><td colspan=3>'.$row['department'].'</td>'.'<td>'.$row['hours'].'</td><td>'.$row['month'].'</td></tr>';
so you should really remove your 2nd while/if statement... since it achieves nothing at the moment

Because js script at the end navigates you away too quickly you have no chance to see what happened! Remove it until you fix things the way you like it, and make sure that js is doing exactly what you are asking for.

Related

How to apply style in an echo'ed <td>?

this is my PHP code:
$query="SELECT name, surname, address FROM employee";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) {
echo '<tr>';
foreach($row as $field) {
echo '<td class="element">' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
And this is my CSS:
.element{
color:red;
}
But fields displayed in my HTML page aren't red. Why it doesn't work?
I already tried with
echo "<td class='element'>" . htmlspecialchars($field) . "</td>";
and
echo '<td class=\"element\">' . htmlspecialchars($field) . "</td>";
but with no success.
I suggest to specify element in your css.
td.element{
color:red;
}
Also it's a good idea to give your "tr" an ID
"tr id="myTR"...
#myTR td.element{
color:red;
}
then try this.
echo '<td style="color:red">' . htmlspecialchars($field) . '</td>';
even this 1 below shold work
echo '<td class="element">' . htmlspecialchars($field) . '</td>'; // for this just dont remove your css class
you just copy and paste to replace your code. and remove that class in css
the issue of mysl_ does not affect that not to display, i am using latest php engine and they work fine, even though we need to change.

line break after each row retrieval

I need to add a line break after each row retrieved;
I hav tried adding in every possible way.. No luck..
while ($line = mysql_fetch_array($result))
{
?>
<style>
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
</style>
<?php
$sender=$line["sender"];
$classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
$q=mysql_query("select * from users where u_id='$sender'");
$row=mysql_fetch_array($q);
$receiver=$row['firstname'];
//if($receiver!= $line["receiver"])
$msg = $msg . "<tr class='$classy'>" .
"<td>" . $receiver . ": </td>" .
"<td>" . $line["msg"] . "</td>"."<td class='date'>" . $line["chattime"] . "</td></tr>";
}
$msg=$msg . "</table>";
echo $msg;
I need to add a break after each sender+msg+time.//like in a usual chat.
Your problem come from float declarations. They are useless in your code :
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
Remove them and it will work as expected.
You don't need them in your code if you use <table> and <tr>. Default behaviour of <tr> is to create a new "line" each time you add it, but with adding float you're breaking this behaviour.
Also, follow #bulforce recommendation to move your CSS before loop : your code add it as many time you have messages in your document (if you have 10 messages, css declarations will be added 10 times).
<style type="text/css">
.whiteBackground { background-color:#F5ECCE; }
.grayBackground { background-color:#CED8F6; }
.date { font-size:10px; color:#627d79; }
</style>
<?php
while ($line = mysql_fetch_array($result))
{
// ...
}
?>
Just try to add a <tr><td colspan="3"> </td></tr> after </tr> inside the while loop
First consider moving the css declaration before the loop, there is no point to have it in the loop body.
Second the second query in the loop should also be moved before the loop and rework this a bit.
Now to your question, one option would be to change the table with divs and spans.. something like so:
<div class="message-row">
<span class="message-user">Name</span>
<span class="message-text">Message</span>
</div>
Then style as you did before, and add margin-bottom: 15px; to the message-row definition.
before while
$msg="<table>";
Inside while
$msg.= "<tr class='$classy'>" in place of $msg = $msg . "<tr class='$classy'>"
Will work for you.
I solved it by replacing table tags with div tags.
Thnku #bulforce; now I will completely follow ur recommendations.
$msg="<div>";
$date_temp="nil";
while ($line = mysql_fetch_array($result))
{
?>
<style>
.whiteBackground { background-color:#F5ECCE;float:left;}
.grayBackground { background-color:#CED8F6; float:right;}
.date { font-size:10px; color:#627d79;float:right}
</style>
<?php
$sender=$line["sender"];
$classy=($sender == $uid)? 'whiteBackground': 'grayBackground';
$q=mysql_query("select * from users where u_id='$sender'");
$row=mysql_fetch_array($q);
$receiver=$row['firstname'];
//if($receiver!= $line["receiver"])
/*if($date_temp!=$line["chatdate"]){
$date_temp=$line["chatdate"];
$msg=$msg.$line["chatdate"];
}*/
$msg = $msg . "<div class='$classy'>" . $receiver.": " . $line["msg"] ."<div class='date'>" . $line["chattime"] . "</div></div></br></br>";
}
$msg=$msg . "</div>";
echo $msg;

PHP/MySQL - Trying to Pass Query Variables From MySQL Fetch Array Through Page URL

I'm running a script that fetches a row from a MySQL table and is supposed to then pass certain variables from that row to the next page to be used in a form that allows user updating.
Here's the excerpt from the script that is trying to pass the variables to the next page:
if ($row['home_score'] == '0' && $row['away_score'] == '0') {
echo '<td><img src="images/report_icon.png" alt="Report Score" /></td>';
}
If I omit everything after "&game_id=" in the href, it displays fine. However, once I start adding the variables, it cuts off the function and stops displaying the page.
Am I doing something simple wrong with the syntax? I've tried playing around with different ways to write it, but to no avail. Do I need to utilize a http_build_query() to make this work?
Here's the entire script code if you need more info:
<?php
// Connect to the database:
require ('../mysqli_connect.php');
// Make the query for games from the schedule database and determine the game location:
$q = "SELECT tl.game_date, tl.game_time, tl.away_team, tl.home_team, tl.home_score,tl.away_score, tl.arbiter_id, us.football_location, us.football_map
FROM test_league tl
INNER JOIN user_schools us ON (tl.home_team = us.school_name)
ORDER BY tl.game_id";
$r = mysqli_query($db, $q);
// Declare two variables to help determine the background color of each row:
$i = 0;
$bgcolor = array('row1', 'row2');
// Begin function to print each game as a row:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr class="' . $bgcolor[$i++ % 2] .'"><td>' . $row['game_date'] . '</td><td>' . $row['game_time'] . '</td><td class="alignleft">' . $row['away_team'] . ' vs<br>' . $row['home_team'] . '</td>';
// Determine if the score has been reported:
if ($row['home_score'] == '0' && $row['away_score'] == '0') {
echo '<td><img src="images/report_icon.png" alt="Report Score" /></td>';
} else {
echo '<td>' . $row['home_score'] . '<br>' . $row['away_score'] . '</td>';
}
echo '<td>' . $row['football_location'] . '</td><td>' . $row['arbiter_id'] . '</td></tr>';
}
mysqli_free_result ($r);
mysqli_close($db);
?>
Any and all advice is greatly appreciated!
Try this, you've got your speech marks and dots mixed up :)
if ($row['home_score'] == '0' && $row['away_score'] == '0') {
echo '<td><img src="images/report_icon.png" alt="Report Score" /></td>';
}
Hope this helps!
$url = 'www.example.com?' . http_build_query($row,'','&');

PHP and HTML Echo

I want to query a result from a table. The problem I run into is if the text is very long it will not auto-wrap to another line"best way i can think to explain it". It displays the text on one line and will go way off to the side of the page if the text is long. How can I get the text to space correctly. Thanks
here is my code:
<?php
echo "<div class=\"item_list2\">";
$get_items = "SELECT * FROM items WHERE category='Art'";
$result = mysql_query($get_items);
//loop! loops through the multiple results of the $get_items query
while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<b>" . $item['item'] . "</b><br/>" . $item['email'] . "<br/>";
echo $item['price'] . "</b><br/>" . $item['category'] . "</b><br/>" . $item['extra'];
echo "<br/><a href='manage.php?delete=" . $item['id'] . "'><small>[Delete]</small></a><br/><br/>";
}
echo "</div>";
?>
Just use CSS to break it.
.container
{
word-wrap: break-word;
}
maybe you want to add..
nl2br($item['extra']);

PHP looping problem?

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.

Categories