Log in

View Full Version : Display data from database in a table with different colours



smithster
05-30-2007, 01:20 PM
I already have a database with information stored in it. And I want to display the information on a webpage using PHP.

Is it possible to tell it to be displayed with the first letter in one colour and the rest of the word in another colour.

So, for example, my name on this site is Smithster.

If this word was stored in a database and I wanted to display it on the webpage in the following format......

Smithster

Is this possible and if so, anyone know how to do it?!?!?!?

Thanks in advance.

Smithster.

Twey
05-30-2007, 01:46 PM
<style type="text/css">
.name-firstletter {
color: red;
}
.name-rest {
color: #48d1cc;
}
</style>
</head>
<body>
<p>
<span class="name-firstletter">
<?php echo($name[0]); ?>
</span>
<span class="name-rest">
<?php echo(substr($name, 1)); ?>
</span>
</p>

Twey
05-30-2007, 01:48 PM
Argh, no! Don't use a separate <span> for every single letter! That's very far from an easiest way, and is ridiculously inefficient.

DimX
05-30-2007, 02:23 PM
Why multiple spans?


<style type="text/css">
.name {
color: #48d1cc;
}
.name:first-letter {
color: red;
}
</style>
</head>
<body>
<p>
<span class="name"><?php echo $name; ?></span>
</p>


Edit
Oh, I see why, first-letter pseudo class doesn't work for span (at least not in IE).

Twey
05-30-2007, 03:08 PM
:first-letter doesn't work at all in IE.

DimX
05-30-2007, 04:29 PM
It does, it works for <p>.

Twey
05-30-2007, 04:33 PM
Oh no, it does work, but has bugs (http://www.satzansatz.de/cssd/pseudocss.html).

smithster
05-30-2007, 05:21 PM
ok, so now Im really confused!!! The original coding you posted Twey...will it work in IE or not??? And how do I put it into a PHP script. As when I copy it in as it is, but change the $name to what I need, I just get an error....

Parse error: syntax error, unexpected '<' in /home/******/public_html/clanapp/challengeus/viewreportwithname.php on line 40

Twey
05-30-2007, 05:24 PM
ok, so now Im really confused!!! The original coding you posted Twey...will it work in IE or not???Mine will work fine, yes. It's DimX' that has issues in IE.
Parse error: syntax error, unexpected '<' in /home/******/public_html/clanapp/challengeus/viewreportwithname.php on line 40The script starts from outside PHP parsing mode. That is to say, it should not be within <?php ?> tags.

smithster
05-30-2007, 06:23 PM
yeah I guessed that!! Have changed it and now the script works again.

But, I still have to get the variable $name to match up with the name in the database. I thought it would be something like this....



$name = . $row['alias'] .


But it does not want to work!! So I am wrong!!

djr33
05-30-2007, 06:44 PM
Those dots aren't helping.

$name= $row['alias'];

smithster
05-30-2007, 06:55 PM
ok thank you so much guys for your help. Just one more thing though.

The output of the name works. But there is a space between the first and second letter!!! I can't find where in the script to correct that?!?!?

Twey
05-30-2007, 07:13 PM
It's just because there's whitespace between the two <span> tags. Remove it, and all will be fine.

smithster
05-30-2007, 07:32 PM
lol thank you, silly me!!!

smithster
05-30-2007, 08:07 PM
ok, the script is complete and it leads me onto another question. Lets say I have 10 names I want outputting into a table. I have just been trying by myself by the way!! But I am failing!!

Any ideas would be a help.

Thanks.

Twey
05-30-2007, 09:18 PM
How are they stored?

smithster
05-30-2007, 09:34 PM
Ive created a new thread which sort of runs off the previous one because I thought it is in a way a different question to the last one even though the two are related so I hope that this is ok.

Basically, I have this code that retrieves data from a table in a database.



<?php
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'dbname';
mysql_select_db($dbname);

$query = "SELECT alias FROM challengeus";
$result = mysql_query($query)
or die(mysql_error());
print "
<center>
<table border=\"5\" cellpadding=\"5\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#808080\" width=\"20%\" id=\"AutoNumber2\" bgcolor=\"#C0C0C0\"><tr>
<td align=center bgcolor=\"#ffffcc\"><b>Player:</b></td>
</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
print "<tr>";
print "<td align=center>" . $row['alias'] . "</td>";
print "</tr>";
}
print "</table>";
print "</center>";
?>


and I have this code that outputs data from the database onto a webpage but into 2 different colours....eg.

Smithster



<style type="text/css">
.name-firstletter {
color: red;
}
.name-rest {
color: #48d1cc;
}
</style>
</head>
<body>
<p>
<span class="name-firstletter">
<?php echo($name[0]); ?>
</span>
<span class="name-rest">
<?php echo(substr($name, 1)); ?>
</span>
</p>


when I put the 2 scripts together, I end up with this....



<html>
<style type="text/css">
.name-firstletter {
color: red;
}
.name-rest {
color: #48d1cc;
}
</style>
<?php
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'dbname';
mysql_select_db($dbname);

$query = "SELECT alias FROM challengeus";
$result = mysql_query($query)
or die(mysql_error());
print "
<center>
<table border=\"5\" cellpadding=\"5\" cellspacing=\"0\" style=\"border-collapse: collapse\" bordercolor=\"#808080\" width=\"20%\" id=\"AutoNumber2\" bgcolor=\"#C0C0C0\"><tr>
<td align=center bgcolor=\"#ffffcc\"><b>Player:</b></td>
</tr>";

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
print "<tr>";
print "<td align=center>"<span class=\"name-firstletter\"><?php echo($row[0]); ?> </span> <span class=\"name-rest\"> <?php echo(substr($row, 1)); ?> </span>"</td>";
print "</tr>";
}
print "</table>";
print "</center>";
?>

smithster
05-30-2007, 09:36 PM
New thread created as this is really a different question. I hope the question I have asked answers this question!!

http://www.dynamicdrive.com/forums/showthread.php?p=94765#post94765


MODERATOR EDIT: This is the same discussion, so please only use one thread. It has already been discussed, so anyone who helps you may wish to review this thread when doing so. I merged the two threads. Please keep it this way, for this, and for future questions.

thetestingsite
05-30-2007, 09:44 PM
The code (once put together) should look like so:



<html>
<style type="text/css">
.name-firstletter {
color: red;
}
.name-rest {
color: #48d1cc;
}
</style>
<?php
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpass';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'dbname';
mysql_select_db($dbname);

$query = "SELECT alias FROM challengeus";
$result = mysql_query($query)
or die(mysql_error());
?>
<center>
<table border="5" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#808080" width="20&#37;" id="AutoNumber2" bgcolor="#C0C0C0"><tr>
<td align=center bgcolor="#ffffcc"><b>Player:</b></td>
</tr>

<?php

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td align=center><span class="name-firstletter"><?php echo $row[0];?></span> <span class="name-rest"><?php echo substr($row, 1));?></span></td>
</tr>
<?php
}
?>
</table>
</center>


Notice how I broke out of PHP parsing mode to basically make it easier to keep track of. Hope this helps.

smithster
05-30-2007, 09:56 PM
Well the script caused an error when first ran. So I put the exact HTML for the span section into it and this time there was no error. But only the top part of the table is shown. Nothing else. Something is still wrong?!?!?

smithster
05-30-2007, 10:02 PM
My mistake, the scripting is correct, and it does output further rows, I forgot to change a variable!!

However, the output is wrong. For some reason, it only outputs for each row under "Player" the word "rray". I have no idea where this word has come from. It certainly isn't in the field in the database. It outputted it 3 times which is correct as there are 3 names stored.

Any ideas?

smithster
05-30-2007, 10:35 PM
I just tried something. Declaring the variable in the span statement, and this outputted a name in the correct way in the table. But it only outputs the last name in the database. Why is this?



while($row = mysql_fetch_array($result, MYSQL_ASSOC))
$name= $row['alias'];

smithster
05-30-2007, 11:04 PM
Ive done it!!! I was using the wrong MYSQL statement.

Here's the corrected code!!!



while(list($alias)=mysql_fetch_row($result))