[PHP] Fetching MySQL Information in PHP
Right now I have...
PHP Code:
<?php
$query = mysql_query("SELECT * FROM vouches");
echo "<table><tr><td>Vouch</td><td>Date Posted</td></tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr><td>";
echo $row['text'];
echo "</td><td>";
echo $row['accepted'];
echo "</td></tr>";
}
echo "</table>";
?>
But in the MySQL database, "accepted" is either a value of 1 (for accepted) or 0 (for pending). Instead of showing the number, I'd like it to show the word "Accepted" or "Pending". How can I do this?
Also, for another part...
There's a row called ID, and one called UID for the sender and receiver of the message. Instead of showing the ID number, I'd like it to search in the users table to show the username that corresponds to that ID.
Edit: After this, when there are like 50 things let's say, there needs to be a "next page" option. How can I do this?
Re: [PHP] Fetching MySQL Information in PHP
PHP Code:
if($row['accepted'] == 0){
echo "Pending.";
}elseif($row['accepted'] == 1){
echo "Accepted.";
}
As for the other part, query the users table for the ID and echo the name.
Look up "php pagination" on google for the pages.
Re: [PHP] Fetching MySQL Information in PHP
Thank you Ron!
PHP Code:
<?php
$query = mysql_query("SELECT * FROM vouches");
echo "<table><tr><td>Vouch</td><td>Date Posted</td><td>Status</td></tr>";
while($row = mysql_fetch_array($query))
{
echo "<tr><td>";
echo $row['text'];
echo "</td><td>";
echo $row['posted'];
echo "</td><td>";
if($row['accepted'] == 0){
echo "Pending";
}elseif($row['accepted'] == 1){
echo "Accepted";
}
echo "</td></tr>";
}
echo "</table>";
?>
I now have this. Still looking for the other two things.
Re: [PHP] Fetching MySQL Information in PHP
$var = ($a == 1 ? 'Accepted' : 'Pending')
Shorthand if statement.
Use the mysql LIMIT statement for paging.
Posted via Mobile Device
Re: [PHP] Fetching MySQL Information in PHP
PHP Code:
$status = array("pending", "accepted");
echo $status[$row['accepted']];
Re: [PHP] Fetching MySQL Information in PHP
PHP Code:
echo ($a == 1 ? 'Accepted' : 'Pending')
wouldn't that work? It's just one line and I don't see why it wouldn't work o_O.
Just curious, not familiar with PHP.
Re: [PHP] Fetching MySQL Information in PHP
both "if" ways, shorthand or not are bad
array is much more extensible, imagine you'll have to make one more status, say, "denied"..
Re: [PHP] Fetching MySQL Information in PHP
Quote:
Originally Posted by
foxx
both "if" ways, shorthand or not are bad
array is much more extensible, imagine you'll have to make one more status, say, "denied"..
In this specific scenario, if is all that is required, because he only has 0 and 1 held in the database.
If he ever plans to extend that to include, say, 'denied' (being 2 in the database), then a switch or your array method would work quite well.
The array would work in less lines than the switch if the database row 'accepted' was only an integer. Otherwise, a switch would be a better idea imo.
Re: [PHP] Fetching MySQL Information in PHP
switch is the worst idea no matter what, array is better even if there are only two options..
and you obviously can do, if accepted was string a "shit" in database, this
PHP Code:
$array = array("shit"=>"Accepted");
Re: [PHP] Fetching MySQL Information in PHP
Quote:
Originally Posted by
foxx
switch is the worst idea no matter what, array is better even if there are only two options..
and you obviously can do, if accepted was string a "shit" in database, this
PHP Code:
$array = array("shit"=>"Accepted");
Good point, I forgot you could initiate arrays like that.
What is your reasoning behind disliking switch so much? Although using an array is less lines in many scenarios, using a switch is, in my opinion, more readable and more organized.
Re: [PHP] Fetching MySQL Information in PHP
I believe switch statement are slow?
Not to mention they only support integers..
Re: [PHP] Fetching MySQL Information in PHP
Quote:
Originally Posted by
Lapje
I believe switch statement are slow?
Not to mention they only support integers..
PHP switch statements support strings and integers.
EDIT: I looked up an answer to my own question.
I found a PHP benchmark of the array mapping that foxx was showing, versus using switches. The results are painful.
http://technosophos.com/content/micr...ments-are-slow
It's good to know. I will be more wary of using switches from now on.
EDIT 2: Just did my own PHP benchmarking, using methods similar to http://phpbench.com, but I iterated through them 100 million times instead of 1 million times, for more accurate numbers.
My benchmark results are thus...
Quote:
Lower numbers = Faster & Better
If/Elseif/Else Benchmark Result: 7.6436998844147
Switch Case/Default Benchmark Result: 11.337376117706
Note: Tests are done over 100,000,000(100 million) iterations.
Numbers returned are gotten via PHP's microtime function.
Using this script...
PHP Code:
<?php
function testIf()
{
$i = 0;
$t = microtime(true);
$answer = 2;
while ($i < 100000000) { //100 million
if($answer === 1) {
} else if($answer === 3) {
} else {
}
$i++;
}
return (microtime(true) - $t);
}
function testSwitch()
{
$i = 0;
$t = microtime(true);
$answer = 2;
while ($i < 100000000) { //100 million
switch ($answer) {
case 1:
break;
case 3:
break;
default:
break;
}
$i++;
}
return (microtime(true) - $t);
}
echo '<i>Lower numbers = Faster & Better</i>';
echo '<br><b>If/Elseif/Else Benchmark Result:</b> ' . testIf();
echo '<br><b>Switch Case/Default Benchmark Result:</b> ' . testSwitch();
echo '<br><i>Note: Tests are done over 100,000,000(100 million) iterations.<br>Numbers returned are gotten via PHP\'s microtime function.</i>';
?>
Re: [PHP] Fetching MySQL Information in PHP
Quote:
Originally Posted by
Lapje
I believe switch statement are slow?
Not to mention they only support integers..
Not sure where you learned PHP at, but they support both.
The only time I use cases is when I need two or more values to fall through to the same code. I prefer it over writing "|| (or)" in IF statements sometimes.
Re: [PHP] Fetching MySQL Information in PHP
Quote:
Originally Posted by
timebomb
Good point, I forgot you could initiate arrays like that.
What is your reasoning behind disliking switch so much? Although using an array is less lines in many scenarios, using a switch is, in my opinion, more readable and more organized.
switch is slow and barely more readable than
PHP Code:
$statuses = array(
5=>'Accepted',
7=>'Pending',
12=>'Denied'
);
echo $statuses[$row['accepted']];
PHP Code:
$status = null;
switch ($row['accepted']) {
case 5:
$status = 'Accepted';
break;
case 7:
$status = 'Pending';
break;
case 12:
$status = 'Denied';
break;
default:
die("Shit happened");
break;
}
echo $status;
Re: [PHP] Fetching MySQL Information in PHP
if/then should be faster than any other solution so long as the number of cases is small (fewer chances to branch). Switches in PHP are almost certainly implemented as an associative array, so they will have a cost with few cases but with more will be quite efficient. I'd avoid using them as much as possible, though.
That said, you should never be doing performance-critical computation in PHP anyway. If you are you're probably doing something wrong. The simplest form of cloud computing available to all of us is moving your data-manipulation code into client-side javascript. The ultimate goal for the back-end in PHP and any other server-side scripting language is to be a very thin service that implements minor data formatting, security, and in some cases caching logic only.
Re: [PHP] Fetching MySQL Information in PHP
Quote:
Originally Posted by
Ron
Not sure where you learned PHP at, but they support both.
The only time I use cases is when I need two or more values to fall through to the same code. I prefer it over writing "|| (or)" in IF statements sometimes.
I don't, just thought it was the same in all languages.