PDA

View Full Version : css formatting a mysql relational query



Skip_B
Jul 19th, 2009, 09:43 PM
I am using php to query mysql to deliver lists from a relational database using a while loop.
I have a database "taverns" then a list of "players" for each tavern. I want the list of players to indent but not the taverns.
I tried a div for each section and the first tavern did not indent but the rest did.
I tried a style on the result line only but it didn't do anything.
I tried inserting a div inside the php code but that produced and error.
Here is the snippet of php
<?php

$tavern_set = mysql_query("SELECT * FROM taverns", $connection);
if (!$tavern_set) {
die("Database query failed: " . mysql_error());
}

while ($tavern = mysql_fetch_array($tavern_set)) {
echo $tavern["id"]." ".$tavern["tavern"]."<br />";
$player_set= mysql_query("SELECT * FROM players
WHERE tavern_id = {$tavern["id"]}", $connection);
if (!$player_set) {
die("Database query failed: " . mysql_error());
}
while ($player = mysql_fetch_array($player_set)) {
echo $player["id"]." ".$player["name"]."<br />";
}
}
?>
It is the last echo set (in red) I am trying to indent without indenting the first echo set (in blue).

brandon1
Jul 19th, 2009, 11:49 PM
<?php

$tavern_set = mysql_query("SELECT * FROM taverns", $connection);
if (!$tavern_set) {
die("Database query failed: " . mysql_error());
}
echo "<ul>";
while ($tavern = mysql_fetch_array($tavern_set)) {
echo "<li>".$tavern["id"]." ".$tavern["tavern"]."";
$player_set= mysql_query("SELECT * FROM players
WHERE tavern_id = {$tavern["id"]}", $connection);
if (!$player_set) {
die("Database query failed: " . mysql_error());
}
echo "<ul>";
while ($player = mysql_fetch_array($player_set)) {
echo "<li>".$player["id"]." ".$player["name"]."</li>";
}
echo "</ul></li>";
}
echo "</ul>";
?>

This isn't really css, but I can see the issue I hope. The only problem with this code, if a tavern doesn't have any players in it, the <li> for that tavern will read as follows "<li>ID tavernName <ul></ul></li>" not really a big deal, but thats what to expect

hope i was helpful

Skip_B
Jul 21st, 2009, 02:33 AM
Thanks Brandon. I have this solution already and I know it works. I guess I am trying to be a purist and go with all css. It seems like there are some tricks to combining it with MySql and php. php doesn't seem to like to intermix like it can with html.

I may be a little anal here. lol. I am just learing all of this, php, css, MySQL and putting it all together. I finally learned how to avoid using tables with css and like it but never would have thought to create so many unnamed divs. I could use that technique but I can't step into the query as it comes out.

VIPStephan
Jul 21st, 2009, 03:08 AM
Thanks Brandon. I have this solution already and I know it works. I guess I am trying to be a purist and go with all css.

CSS alone doesn’t do anything, it needs some HTML that it can style. That said: Can you show us the HTML output (as seen in the browser’s “view source” mode)? Basically the HTML you use depends on the meaning the output has. I’d even suspect you actually want a table here or at least a list. And this code can then be styled with CSS.

DaiLaughing
Jul 21st, 2009, 09:31 AM
The very important Stephan makes a very important point. Brandon's HTML seems fine and your PHP is close but there is no CSS there. This is fine as all three have a different job:


PHP provides the content
HTML provides the structure (a list makes sense)
CSS provides the formatting if browser defaults are no good


To improve the PHP I suggest that you add an IF statement which checks for any content (as brandon pointed out) and if there is no content it inserts $nbsp; so that the LI has something in it (or use "no players" if you want).

Have a look at the end page in a few browsers and if you don't like the look use CSS to change it in a stylesheet maybe using classes.

Skip_B
Jul 21st, 2009, 08:51 PM
I think I am trying to use css where it is not a fit. The UL solution works fine.
I am having the same trouble trying to control positioning of a php based mysql query. css doesn't seem to be able to control php. In Expression 2 design view I see a nicely structured table based on divs but what displays in the browser is all of the MySql queries stacked on top of each other and nothing I do in css seems to affect them.

I think a basic table or ul is the answer possibly with css acting on the table or list if I need to format it further.

Is this a better understanding of the role of css?

DaiLaughing
Jul 22nd, 2009, 08:30 AM
Better, I think. CSS will never control PHP because PHP is processed on the server. When it is processed it generates the HTML code which is sent to the browser. Then the CSS is applied to the HTML.

It is wise to look at the HTML using the View Source (or similar) feature on a browser by right clicking on a blank area of the page. That means there is no PHP to confuse you. Different browsers show the code in different ways but I find Firefox is best at colour coding.