Jump to content

[SOLVED] What am I missing?


l3asturd

Recommended Posts

I can't get this to SORT my results by Name or Score. Can anyone see what I am doing wrong? Sorry if the code's a mess, I'm very new. Any improvements or fixes would be appreciated. Thanks! ???

 

<?php
// database information - set this to what your database login information is
$host = 'hidden';
$user = 'hidden';
$password = 'hidden';
// This is the name of the database you will be connecting to. Might want to name it Tournaments or something
$dbName = 'DVDB3';
$dbTable = 'TLB';

// connect and select the database
$conn = mysql_connect($host, $user, $password) or die(mysql_error());
$db = mysql_select_db($dbName, $conn) or die(mysql_error());
// Pull the results from the table
$select_result = mysql_query("SELECT * FROM $dbTable ORDER BY 'Score' ASC", $conn);
{
// The rest of this loops through outputting the information
echo <<<FORMENT
<table cellpadding=0 cellspacing=0 width=200 border=1>
<th align=center width=100>Name</th>
<th align=center width=50>Score</th>
<th align=center width=50>Winnings</th>
FORMENT;
}
while($list1 = mysql_fetch_assoc($select_result))
{
echo <<<EOF
<tr border=2 bgcolor=silver>
<td align=center width=100>{$list1['Name']}</td>
<td align=center width=50>{$list1['Score']}</td>
<td align=center width=50>{$list1['Purse']}</td>
</tr>

EOF;
}{echo <<<FORMENT
</table>
FORMENT;

}
// Make sure the user entered values for all three fields
if (strlen($_POST['name'])>0 && strlen($_POST['score'])>0 && strlen($_POST['purse']))
// IF TRUE
{
// Add the values to the $data array
$data = array(
'Name' => $_POST['name'],
'Score' => $_POST['score'],
'Purse' => $_POST['purse'],
);
$field_names = array();
$values = array();
// Loop through the array
foreach($data as $field_name => $value)
{
if(empty($value)) continue;

$field_names[] = $field_name;
// Make sure no invalid information was put into the form
$values[] = "'".mysql_real_escape_string($value, $conn)."'";
}
// Add the results into the table
$insert_result = mysql_query("INSERT INTO $dbTable (".join(',',$field_names).") VALUES(".join(',',$values).")", $conn);
echo "<Font color=FF0000><b>Added  data to TLB, please <i>REFRESH</i></b><br></font>";
}
$DELETEYES = ($_POST['cleartable']);
if (($DELETEYES) == "1")
{
$delete_result = mysql_query("DELETE FROM $dbTable", $conn);
echo "Table cleared, please <b><i><font color=red>REFRESH</i></b></font><br>";
}
{
}
{
echo <<<FORMENT
<br> Make a new entry: <br> 
<form action = "{$_SERVER['PHP_SELF']}" method = "post"> 
<input type = "text" name = "name" maxlength="20" size = "10"> <br>
<input type = "text" name = "score" maxlength="4" size = "10"> <br>
<input type = "text" name = "purse" maxlength="5" size = "10"> <br>
<input type = "submit" value = "Add Entry"> 
<input type= "checkbox" name = "cleartable" value="1">CLEAR TABLE? 
<input type="submit" value = "Refresh">
</form> 
FORMENT;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/62812-solved-what-am-i-missing/
Share on other sites

<?php
// database information - set this to what your database login information is
$host = 'hidden';
$user = 'hidden';
$password = 'hidden';
// This is the name of the database you will be connecting to. Might want to name it Tournaments or something
$dbName = 'DVDB3';
$dbTable = 'TLB';

// connect and select the database
$conn = mysql_connect($host, $user, $password) or die(mysql_error());
$db = mysql_select_db($dbName, $conn) or die(mysql_error());
// Pull the results from the table
$select_result = mysql_query("SELECT * FROM $dbTable ORDER BY Score ASC", $conn);

// The rest of this loops through outputting the information
$content = "";
$content .= "<table cellpadding=0 cellspacing=0 width=200 border=1>";
$content .= "<th align=center width=100>Name</th>";
$content .= "<th align=center width=50>Score</th>";
$content .= "<th align=center width=50>Winnings</th>";

while($list1 = mysql_fetch_assoc($select_result)):
$content .= "<tr border=2 bgcolor=silver>";
$content .= "<td align=center width=100>".$list1['Name']."</td>";
$content .= "<td align=center width=50>".$list1['Score']."</td>";
$content .= "<td align=center width=50>".$list1['Purse']."</td>";
$content .= "</tr>";
endwhile;

$content .= "</table>";

// Make sure the user entered values for all three fields
if (strlen($_POST['name'])>0 && strlen($_POST['score'])>0 && strlen($_POST['purse'])):// IF TRUE

// Add the values to the $data array
$data = array('Name' => $_POST['name'],'Score' => $_POST['score'],'Purse' => $_POST['purse']);
$field_names = array();
$values = array();
// Loop through the array
foreach($data as $field_name => $value):
	if(empty($value)) continue;
	$field_names[] = $field_name;
	// Make sure no invalid information was put into the form
	$values[] = "'".mysql_real_escape_string($value, $conn)."'";
endforeach;
// Add the results into the table
$insert_result = mysql_query("INSERT INTO $dbTable (".join(',',$field_names).") VALUES(".join(',',$values).")", $conn);
$content .= "<Font color=FF0000><b>Added  data to TLB, please <i>REFRESH</i></b><br></font>";
endif;

if ($_POST['cleartable'] == "1"):
$delete_result = mysql_query("DELETE FROM $dbTable", $conn);
$content .= "Table cleared, please <b><i><font color=red>REFRESH</i></b></font><br>";
endif;

$content .= "<br> Make a new entry: <br>";
$content .= "<form action=\"{$_SERVER['PHP_SELF']}\" method = \"post\">";
$content .= "<input type=\"text\" name=\"name\" maxlength=\"20\" size=\"10\"><br>";
$content .= "<input type=\"text\" name=\"score\" maxlength=\"4\" size=\"10\"><br>";
$content .= "<input type=\"text\" name=\"purse\" maxlength=\"5\" size=\"10\"><br>";
$content .= "<input type=\"submit\" value=\"Add Entry\">";
$content .= "<input type=\"checkbox\" name=\"cleartable\" value="1">CLEAR TABLE? ";
$content .= "<input type=\"submit\" value=\"Refresh\">";
$content .= "</form>";
echo $content;
?>

DeadEvil I can't see what changes you made that might have fixed the ORDER BY. I see you assigned the output to $content. I also see you put quotes around all my HTML variables(Like there should be). But for some reason, it's now sorting and I don't know why. Any help here?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.