Everything is stacking together? - php

I am here once again with a problem. [:S]
Everything is stacking together!
Here's the code:
while ($row = mysql_fetch_array($data)) {
echo "<div id='blPost'>".
"<a href='read.php?pageid={$row['id']}'>".
"{$row['title']}</a> | Date: {$row['date']}".
"<div class='sep' />";
$sbstS = substr($row['data'],0,500);
echo $sbstS;
echo '</div>';
The problem is, everything is stacking together! If someone could help, here's my testing server. This is the development server I'm using. I've already fiddled with it a few times. This is a 1 day outdated version of the CMS. It uses the same functions and everything, but missing the images, such as the potatoes. Thanks.

its bunched because of the height attribute in the .sep class in your css

Your first blPost div is never closed.
<div class="sep" />
is not valid. You need to close your divs correctly.

You should to use
mysql_fetch_assoc($data)
for using
$row['id']
that syntax.
http://php.net/manual/en/function.mysql-fetch-assoc.php

Related

How do I echo slashes in php?

Everytime I check the HTML output of this piece of code, the slashes aren't included. Thus, the background image fails. I even put it through a html to php converter. Im lost; please help.
while($row = mysql_fetch_array($data))
{
//Echo Theme Template on pages
echo "<div style='background-image:url('../uploads/avi/{$row['avi']}')></div>";
echo "<div class='myname'>{$me}</div>";
}
the simplest answer would be, you have an unclosed ' on your style attribute..
echo "<div style='background-image:url('../uploads/avi/{$row['avi']}')'></div>";
^here
but this wouldn't work as is.. so you should adjust the quotes like this:
echo "<div style='background-image:url(\"../uploads/avi/{$row['avi']}\");'></div>";
you can see the broken echo http://codepad.viper-7.com/CGMdUx
and the edited one is here http://codepad.viper-7.com/bEyKFz
i passed it through htmlspecialchars on codepad just so you can see it as string and avoid being rendered as HTML for viewing purposes only..
You did two things wrong here. You never closed the final quote for the style tag, and using single quotes for both style='' and the url('') cancelled each other out.
I'd recommend always using double quotes for HTML tags.
while($row = mysql_fetch_array($data))
//Echo Theme Template on pages
{
echo "<div style=\"background-image:url('../uploads/avi/{$row['avi']}');\"></div>";
echo "<div class='myname'>{$me}</div>";
}
Another thing to consider, always say "View Source" instead of "Inspect" with Firebug or some other tool. The reason you didn't see the URL printed out is because new browsers are "Smart" and try to fix DOM errors. Inspecting the source with Firebug or a similar tool will show you what the browser actually interprets. Viewing the source will show what was actually sent to the browser.
Because of the inner ' used by the style attribute, you need to use double quotes inside.
echo '<div style="background-image:url(\'../uploads/avi/' . $row['avi'] . "')></div>";
Result
<div style="background-image:url('../uploads/avi/abc.efg')></div>

Array contents not displaying well, when placed between certain lines of the code

Whenever I move the codeblock that generates courses with a grade of "F", it does not not echo out the courses that meets the criteria. But when I move it to beginning of the main div header of the script it displays well.. What could the problem be? I actually want it to be at the end of the script for the sake of printing out. I've also checked the source code and the parameters I was looking for wasn't there.
Pastebin of full code
echo "<table bgcolor = red >";
echo "<tr align= \"center\">";
$carry_over = array();
$score_count = mysql_numrows($query8);
echo "<th>"."Failed Courses : "."</th>";
if($score_count !== 0){
while ($row8 = mysql_fetch_assoc($query8)) {
echo"<td>". $row8['course_code']."</td>;
}
}
echo "</tr>\n";
echo "</table>";
There are inconsistencies between your provided example here and the pastebin code you linked to. Which version are you using?
If it's the pastebin version you may be having issues with this line:
echo "<th>"."Failed Courses";
You are missing the </th> tag which could be messing with your output.
It seems like it is going to be a long bit of code so I'm posting it here, I don't intend for this to be an answer to your problem as I can't get it to do what you're saying.
PHP allows for information to be set outside of the PHP tags, and when that happens it is treated as regular HTML.
E.G.
<?php
If(2==2){
?><b>HELLO!</b><?php
}
?>
And
<?php
if(2==2){
echo "<b>HELLO!</b>";
}
?>
Will both result in <b>HELLO!</b> to be output to the screen. This is useful for things that might require a lot of HTML in more than one block, as well as HTML that may require extensive style definitions or other places where double quotes will be needed, for example
<div id="list_row[$i]" class="something something2 something3"> would have to be escaped as
echo <div id=\"list_row[$i]\" class=\"something something2 something3\"> whereas it could just be put more or less intact in php using the above mentioned fact. Now, there would still need to be an echo statement for the $i portion, as I don't think PHP processes the text, but I've never tried it so I can't be sure.
As for your HTML,
<th> tags are meant to be a header, aka in the top part of a table. <td> are meant to be table-data cells.
Seems like you're making a lot of unnecessary calls to the same MySQL tables, and I'm hoping I can condense it down a bit.
Why are you doing this?
echo "<th>"."Failed Courses : "."</th>";
do
echo "<th>"."Failed Courses : </th>";
and also before you do a while loop try to see what your results looks like.
$row8 = mysql_fetch_assoc($query8)
then print_r($row8);
post your results.
Also the table structure should be
<table>
<th></th>
<tr>
<td></td>
</tr>
</table>

Displaying an image from a directory in PHP

I'm trying to display an image file from a directory using a PHP echo command and an IMG tag.
Here is the code:
//These variables represent the file name extensions from a form element from a previous page
$bannerimg=$_POST["banimg"];
$adimage=$_POST["adimage"];
echo "<img src='imgdir/'".$bannerimg."/>";
When I echo out the file variables ($bannerimg and $adimage) I get the proper file name and extension.
In theory, will this work? If so, what is the proper syntax to handle that echo statement?
Thanks for all the help.
Dustin
Yes it would work, but you should have tested that already.
Alternative syntaxes to the echo statement that I find a little bit more readable would be:
echo "<img src='imgdir/{$bannerimg}/>";
echo "<img src='imgdir/$bannerimg/>";
You can read all about variable parsing in the manual, the first syntax is the complex one and the second the simple. I prefer the complex one as the end of the variable is clearly defined and you can use it for complex expressions, not just simple variables.
You're doing it right but you can use the following just to keep it clean.
echo "<img src='imgdir/$bannerimg' />";
It should work.
To Sandeep's comment, I would have gone the other way.
echo '<img src="imgdir/'.$bannerimg.'/>';
Using " means the parser needs to check to see if there is anything to evaluate.

aligning the name<p align="left">

i wanted to align my Welcome note to the right by using <p align="right">
but it doesnt seem to work..is it because Session/PHP doesnt work with p align?
<?php
session_start();
if($_SESSION['SESS_admin'] == 0)
require("do_menu.php");
else
require("do_menu3.php");
require("auth.php");
require("do_html_header.php");
do_html_header();
print"<p align=\"RIGHT\"><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></p>";
do_menu();
?>
thanks in advance!
Try this
echo "<div style='text-align:right'><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></div>";
It is probably because your <h1> tag is not set to align right and is instead set to align center. You need to override this with CSS. Try this instead: echo "<h1 style="text-align: right;">Welcome ".$_SESSION['SESS_FIRST_NAME']."</h1>"
print"<p align=\"RIGHT\"><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></p>";
You're allowed to use single qoutes, too. It might help you here.
echo '<p align="right"><h1>Welcome '. $_SESSION['SESS_FIRST_NAME'] .'!</h1></p>';
Also, print should have a space after it, or you should be wrapping what's being printed in parenthesis.
Edit: I used echo out of habit, sorry. You can use either, but I [think] I read somewhere that echo is a better choice than print - not 100% sure, though, but they do the same thing. I'll go look it up, though. (Of course, if anyone reading this knows if one is better than the other, let me know!)
The use of ALIGN is deprecated, and fails in many situations. In your case, you're probably writing something in the html_header.php which causes ALIGN to fail.
Try doing:
print "<div style=\"float:right\"><p><h1>Welcome ". $_SESSION['SESS_FIRST_NAME']."!</h1></p></div>";
<div align =right>
<?php
..
..
.. // code you want to align to the right
..
?>
</div>
This way you can align a particular segment of code to the right.
If later you want to align some other segment of code in the center.
Then you can use
This way you can align the different segments differently.

Can I optimize this PHP Script further for Generating a Dynamic Nav from MySQL Database?

I'm working on a new CMS to use on repeative projects. Basically this chunk of code connects to a server, grabs all the names of the tables and uses them to generate a simple navigation. It works pretty great for what I need however I'm just wondering if maybe I can optimize this code sniplet even more and make it even more simple. Maybe making a class that could customize the formating? Etc. I tried to make this as "bare-bones" as possible.
The only thing that is there that I would like to explain is that it checks to see if the table name isn't "includes" this is a default table my CMS uses to know what data to display on the front end as far as data.
<?php
echo '<div class="dynamic_nav_head">Navigation</div>';
echo '<div class="dynamic_nav">';
include('data.php');
$tables = mysql_list_tables($database);
while (list($table) = mysql_fetch_row($tables)) {
if($table!='includes'){
echo "<div class='cat'>".ucwords($table)."</div>";
echo "<div class='cat_item'>";
echo "<a href='?page=read&section=".$table."'>View " . ucwords($table) . "</a>";
echo "</div>";
echo "<div class='cat_item'>";
echo "<a href='?page=add&section=".$table."'>Add New ". ucwords($table) ."</a>";
echo "</div>";
} // End If not in Includes.
} // End While
echo '</div>';
?>
Any Suggestions on how I can make this code even leaner, cleaner, and more swift? Thanks in advance!
Edit:
MySQL Version: 4.1.22
I suggest you to visit http://refactormycode.com/
echo '<div class="dynamic_nav_head">Navigation</div><div class="dynamic_nav">'; // on less echo
include('data.php');
$tables = mysql_list_tables($database);
while (list($table) = mysql_fetch_row($tables)) {
if($table!='includes'){
$ucTable= ucwords($table); // just one function call
// just one echo;
// you where also using quotes and double quotes backwards
echo '<div class="cat">'.$ucTable.'</div><div class="cat_item">View ' .$ucTable.'</div><div class="cat_item">Add New '. $ucTable .'</div>';
} // End If not in Includes.
} // End While
echo '</div>';
How do you know the code is slow? What does your profiler say about the code? Which statement is slowing it down? What platform are you on? What version of mysql? How many tables are in this catalog? Are you suffering from premature optimization?
The code is not bad. One improvement for readability also would be build an array from the database rather than having mysql_fetch_row within the 'while' construct. This would also allow you to filter out the unwanted names before entering the loop. You could also map the ucwords method on the array allowing you to take this out of the while loop construct. The point about the double quotes is a valid one but I would keep seperate echo statements for readability since this is not really going to make an appreciable difference here.
Hope that helps.

Categories