Yup Bingo!
It returned just three results
Is it possible to round the difference of the two dates? - Would this be a good idea?
| SitePoint Sponsor |


Yup Bingo!
It returned just three results
Is it possible to round the difference of the two dates? - Would this be a good idea?


it's already rounded -- down
integer division occurs when you divide one integer by another integer
the answer is an integer
do 14764 / 365 on a calculator... throw away the decimal portion... that's integer division
neat, eh?
![]()


It Works!!!
But now I have another problem.
How do I sort the username's alphabetically, upon clicking on a link?
Cheers for your help, you doing great!![]()




So the link will be something like the following?:
How do I get the link to carry out a query, upon being clicked?Code:echo '<a href="">Sort by First Name<a>'; echo '<a href="">Sort by Last Name<a>';
Last edited by F.Danials; Jul 15, 2008 at 04:38.


I'm Stuck![]()


wish i could help you, but i don't do php
notice, though, that a link like this won't actually do anything --
<a href="">Sort by First Name<a>
This should get you going...
PHP Code:<?php
//--> Your regular SQL statement
$sSQL = 'SELECT * FROM member WHERE DATEDIFF(CURRENT_DATE,DOB)/365 BETWEEN 18 AND 27';
//--> We'll try and detect from the URL if we wish the results sorted.
if( isset($_GET['sortBy']) )
{
switch ($_GET['sortBy'])
{
case 'username':
//--> Adding this to the end of the SQL statement.
$sSQL .= ' ORDER BY username ASC;';
break;
case 'id':
//--> Adding this to the end of the SQL statement.
$sSQL .= ' ORDER BY id ASC;';
break;
default:
//--> Adding this to the end of the SQL statement because nothing else previous was found
$sSQL .= ' ORDER BY username DESC;';
break;
}
}
//--> Prints out the link to sort by username.
echo sprintf('<a href="pagename.php?sortBy=username">Sort By Username</a><br />');
//--> Prints out the link to sort by id.
echo sprintf('<a href="pagename.php?sortBy=id">Sort By Username</a><br />');
?>


OK. Cheers.
But I have one more question for ya:
I'm trying to store a six figure currency (500000) in the database, but each time I enter the figure, the database stores 999.99. Why is this?
Code:$sql = "CREATE TABLE cars ( price decimal(5,2) NOT NULL default '00000.00' )";
AFAIK, it's technically better to put the price as an integer, in pence/cents. I.e. $500,000 == 50,000,000 cents.
But your problem is because the 5 relates to the amount of digits in the whole figure, i.e. 999.99 has 5 digits.
So to put in 500,000.00, you'd need DECIMAL(8,2)
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona


Final thing, how do I create a random number, that is always 6 digits in length? - Obviously, I need to use the rand function, but how do I set the length to always be 10 digits?
Thanks for all ya help. You've been excellent!
rand(100000, 999999)
Or, if you want it to be able to have less than 100,000 but have leading Zeros, you can use sprintf():
PHP Code:<?php
$rand = rand(0, 999999);
$rand = sprintf('%06d', $rand);
?>
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Or if you meant with MySQL:
Code:SELECT FLOOR(RAND() * 1000000);
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona


No the PHP Code version is fine.
If the user clicks a link to sort the data by a certain field in ascending order, is it possible to convert the link so that it then sorts the data in descending order, if clicked a 2nd time?
Yeah.
Code php:<?php $allowedlist = array('username', 'id'); $directionlist = array('asc', 'desc'); $sort = (isset($_GET['sortBy']) && in_array($_GET['sortBy'], $allowedlist)) ? $_GET['sortBy'] : 'username'; $order = (isset($_GET['order']) && in_array(strtolower($_GET['order']), $directionlist)) ? $_GET['order'] : 'asc'; $order = strtoupper($order); $sSQL = "SELECT * FROM member WHERE DATEDIFF(CURRENT_DATE,DOB)/365 BETWEEN 18 AND 27 ORDER BY {$sort} {$order}"; $flag = (($sortBy == 'username' && $order == 'asc') ? true : false); printf('<a href="pagename.php?sortBy=username&order=%s">Sort By Username %s</a><br />', ($flag ? 'desc' : 'asc'), ($flag ? '/\\' : '\\/')); $flag = (($sortBy == 'id' && $order == 'asc') ? true : false); printf('<a href="pagename.php?sortBy=id&order=%s">Sort By ID %s</a><br />', ($flag ? 'desc' : 'asc'), ($flag ? '/\\' : '\\/')); ?>
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona


Quick question, what's wrong with this?:
I get the following error:Code:onClick="window.open("view.php?name=$row['name']",'mywindow','width=400,height=200')"
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in F:\php\htdocs\display.php on line 28
What's the whole line?
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona


Code:echo "<td><img src='".$row['image]."' onClick="window.open("view.php?name=$row['name']",'mywindow','width=400,height=200')"></td>";
PHP Code:echo <<<IMAGE
<td><img src="{$row['image']}" onClick="window.open('view.php?name={$row['name']}','mywindow','width=400,height=200');"></td>
IMAGE;
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona


What does "<<<IMAGE" do?
I know get the following error message:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in F:\php\htdocs\display.php on line 32
Code:echo <<<IMAGE <td><img src="{$row['image']}" onClick="window.open('view.php?name={$row['name']}','mywindow','width=400,height=200');"></td> IMAGE; echo "<td>" . $row['DOB'] . "</td>"; //This is line 32


?
Bookmarks