Page 1 of 1

Can't form a table? Invalid symbols

Posted: Wed Jun 13, 2007 5:23 am
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?

Posted: Wed Jun 13, 2007 5:47 am
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.

Posted: Wed Jun 13, 2007 9:39 am
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.

Posted: Wed Jun 13, 2007 10:56 am
by thefreebielife
so should i put that sql below the script i have above? (after the ?>)

Posted: Wed Jun 13, 2007 11:23 am
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?

Posted: Wed Jun 13, 2007 11:37 am
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>

<? } ?>

Re: Can't form a table? Invalid symbols

Posted: Thu Jun 14, 2007 2:50 am
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?

Posted: Thu Jun 14, 2007 5:00 am
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".