Can't form a table? Invalid symbols

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thefreebielife
Forum Contributor
Posts: 126
Joined: Thu Apr 26, 2007 2:59 pm

Can't form a table? Invalid symbols

Post by thefreebielife »

Code: Select all

<?php
$link = mysql_connect('mysql14.powweb.com', 'aa', 'aa');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('a100');
if (!$db_selected) {
    die('Could not select database: ' . mysql_error());
}
$query = 'SELECT username, ostatus FROM users';
$result = mysql_query($query);
if (!$result) {
    die('Query failed: ' . mysql_error());
}
/* fetch rows in reverse order */
for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
    if (!mysql_data_seek($result, $i)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($row = mysql_fetch_assoc($result))) {
        continue;
    }

 echo $row['username'] . ' ' . $row['ostatus'] . "<br />\n";
}

mysql_free_result($result);
?> 
I want to basically change the echo part into a table.

but whenever i do, i get a cgi error saying theres an unknown "<" when i use a td or tr command.

Sorry, ive never used this kind of code before thats why im confused.

where would i put tr and td commands to form a descending table echoing all my users with their "ostatus" next to them?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

thefreebielife wrote:/* fetch rows in reverse order */
What's the non-reverse order?
mysql can order the records sparing you the need to have extra code for this in your script.
smudge
Forum Contributor
Posts: 151
Joined: Sun May 20, 2007 12:13 pm

Post by smudge »

the sql is:

Code: Select all

SELECT [fields] FROM [table] WHERE [expression] ORDER BY [field] [ASC/DESC]
Where:
[fields] is the fields you are retrieving
[table] is the table you are retrieving from
[expression] is an expression to filter the returned rows (the WHERE [expression] part is optional if you don't want to filter)
[field] is the field you are ordering the values by
[ASC/DESC] is either ASC for ascending order or DESC for descending order.

Note: DON'T use the brackets in the actual sql, they're just place holders.
thefreebielife
Forum Contributor
Posts: 126
Joined: Thu Apr 26, 2007 2:59 pm

Post by thefreebielife »

so should i put that sql below the script i have above? (after the ?>)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Might be decorative.
But no, it wouldn't have much effect there.
$query = 'SELECT username, ostatus FROM users';
$result = mysql_query($query);
mysql returns the records in a certain order. And then
* fetch rows in reverse order */
for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
if (!mysql_data_seek($result, $i)) {
echo "Cannot seek to row $i: " . mysql_error() . "\n";
continue;
}

if (!($row = mysql_fetch_assoc($result))) {
continue;
}

echo $row['username'] . ' ' . $row['ostatus'] . "<br />\n";
}
it takes you a (relatively) immense effort to reverse that order. Mysql can do that for you, it will deliver the records in an order you choose.
What ordering are you trying to achiev?
thefreebielife
Forum Contributor
Posts: 126
Joined: Thu Apr 26, 2007 2:59 pm

Post by thefreebielife »

something similar to what this code produces:

Code: Select all

	<div align="center">
		  <h2>		    <table width="558" height="107" rules="rows" style="font-size:13px;border: 1px solid #0099CC;" cellpadding="3" cellspacing="0">
		  	<tr bgcolor="#6688AA">
			<Td colspan="4" style="border-bottom:1px solid #0099CC; color:#FFFFFF; font-size:15px;"><div align="center"><strong>
			  Spamming Report
			  </strong></div></Td>
			</tr>
		      <tr bgcolor="FFCC99">
		      <Td align="left"><strong>User</strong></Td>
		      <Td align="left"><strong>Reported By</strong></Td>

		      <Td align="left"><strong>Date</strong></Td>
		      <Td align="left"><strong>Action</strong></Td>
		      </tr>

<?

$sql = "SELECT * FROM spamreport order by date DESC";
$result = mysql_query($sql);
$i=0;
while($r=mysql_fetch_array($result))
{	
	$i=$i+1;
?>

		      <tr bgcolor="#<?= ($i%2) ? 'FFE5B7' : 'FFEECC' ?>">
		      <Td>
		      <? echo $r["username"]; ?></Td>
		      <Td>
		      <? echo $r["reportedBy"]; ?></Td>

		      <Td>
		      <? echo $r["date"]; ?></Td>
		      <Td>
		      <? echo "<a href='aspam.php?action=delete&user=" . $r['username'] . "'>Remove</a>"; ?></Td>
		      </tr>
<? }
			 if($i==0)
{
?>
 		      <tr>
		      <Td colspan=4 align="center"><FONT SIZE="2" COLOR="#FF0000"><B>No Users Reported</B></FONT></Td>
		      </tr>

<? } ?>
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Re: Can't form a table? Invalid symbols

Post by Gente »

thefreebielife wrote: I want to basically change the echo part into a table.
but whenever i do, i get a cgi error saying theres an unknown "<" when i use a td or tr command.
Could your provide your "wrong" code?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

thefreebielife wrote:something similar to what this code produces:
I expected an answer like "displaying the records sorted by the username in reverse order".
Post Reply