glennda Posted February 9, 2011 Posted February 9, 2011 I have a php script which i am going to hopefully use for my noticeboard which i have been creating. I have a script which imports to the database. I also have one which is displaying the contents of the mysql table. But i can't seem to work out how to set it up so that it displayes in a nice format. I have this while($result = mysql_fetch_array($query)) { echo $result['subject']; echo $result['name'];echo $result['post']; echo $result['expire'];} but that just is displaying everything. I have tried everything by trying to echo it different things like tables etc. but cant't seem to work out what is wrong!
bladedanny Posted February 9, 2011 Posted February 9, 2011 Could you put the results into a new variable then use the new variable where ever and how ever you'd like to? ---- Ignore this ---- I've thought about it and it woudln't work. I jumped the gun on my suggestion. Sorry.
LosOjos Posted February 9, 2011 Posted February 9, 2011 (edited) Try using 'printf' instead to add some labels (I belive, but I'm not certain, you can also add HTML tags to pretty it up): while($result = mysql_fetch_array($query)) { printf("Subject: %s - Name: %s - Post: %s", $result["subject"], $result["name"], $result["post"]); } The first parameter is your formatted text, with each result represented by %s, the order in which the following parameters are called defines which order they are returned to the first parameter. EDIT: scrap that, I'm talking rubbish again. Just echo in to an HTML table? e.g. </pre><table> while($result = mysql_fetch_array($query)) { echo "$result['subject']$result['name']$result['post']$result['expire']";} </ Edited February 9, 2011 by LosOjos
Hightower Posted February 9, 2011 Posted February 9, 2011 PHP/MySQL just gets data dynamically. You need to use some form of HTML and possibly CSS to pretify it: while($result = mysql_fetch_array($query)) { ?> } ?> Something like that 1
glennda Posted February 9, 2011 Author Posted February 9, 2011 Cheers. I was trying to do it all in the same script!
Hightower Posted February 9, 2011 Posted February 9, 2011 You can echo HTML in the PHP as follows: echo $result['something'] . " and static text or HTML HEADING can go here followed by more PHP " . $result['somethingelse']; But I find it so much easier to read by closing off the PHP tags and using little snippets within the HTML where needed. To echo PHP and static text in the same line just remember to use a '.' between the PHP element and the static. Also, static stuff has to be in quotes of some kind, either " or ' EDIT: Just another tip. When formatting I tend to do this: HEADING HERE SUB HEADING HERE BODY HERE Then once I'm happy the static text looks how I want it to be I go and swap HEADING HERE with <?php echo $result['heading']; ?> and so on.
Hightower Posted February 9, 2011 Posted February 9, 2011 Oh, last bit too. Instead of doing: echo $result['one']; echo $result['two']; echo $result['three']; you can just type echo $result['one'] . $result['two'] . $result['three'];
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now