Page 1 of 2

help please

Posted: Tue Sep 15, 2009 11:36 am
by margaaa
hello,

can anyone help me to resolve this exercise:

"Do a PHP page with a column (named od) in a chart (named numbers) in a database (named MR) with all the od numbers between 0 ro 1000, except the number 501."

pleeease help me
thank you

Re: help please

Posted: Tue Sep 15, 2009 12:47 pm
by Mirge
This some sort of assignment? I wouldn't mind giving a hand... though it is extremely simple. Post some code so we can see what you've done so far and help out.

Re: help please

Posted: Tue Sep 15, 2009 3:03 pm
by peterjwest
Here you are:

Code: Select all

Numbers
<table border="1">
  <tr>
    <th>od</th>
  </tr>
  <tr>
    <td>all the od numbers between 0 ro 1000, except the number 501</td>
  </tr>
</table>
If you'd like help, please at least try it first and post what you've managed so far. Also spend some time checking your spelling and grammar.

And you should know that 'odd' has two d's.

Re: help please

Posted: Tue Sep 15, 2009 3:19 pm
by Mirge
peterjwest wrote:Here you are:

Code: Select all

Numbers
<table border="1">
  <tr>
    <th>od</th>
  </tr>
  <tr>
    <td>all the od numbers between 0 ro 1000, except the number 501</td>
  </tr>
</table>
If you'd like help, please at least try it first and post what you've managed so far. Also spend some time checking your spelling and grammar.

And you should know that 'odd' has two d's.
lol I like how you built the table verbatim.. that's hilarious

Re: help please

Posted: Tue Sep 15, 2009 3:29 pm
by ricehigh
As stated above, you should really give it a try, before handing the problem to someone else.

Though, due to the simplicity of creating a table, I've written a small little script for you. I hope this is what you were looking for:

Code: Select all

<table>
<tr><th>Numbers</th></tr>
<?php
for($x=1;$x<1000;$x++){
    if($x & 1 && $x != 501)
        echo "<tr><td>$x</td></tr>\n";
}
?>
</table>
Please note that $x&1 performs better (read faster) than $x%2.
(This is because $x&1 performs a simple binary comparison, while $x%2 performs a division, giving you the remainder.)

Re: help please

Posted: Tue Sep 15, 2009 3:31 pm
by Mirge
I don't necessarily agree with writing scripts for others. I'd rather them learn how to stand on their own two feet. But to each their own I suppose.

Re: help please

Posted: Tue Sep 15, 2009 3:40 pm
by ricehigh
Mirge: If you read my post, you might have noticed that i agree with you :wink:

Re: help please

Posted: Tue Sep 15, 2009 3:50 pm
by Mirge
ricehigh wrote: Mirge: If you read my post, you might have noticed that i agree with you :wink:
I did read your post... you wrote the script :P.

Re: help please

Posted: Tue Sep 15, 2009 7:10 pm
by califdon
I'm with you, Mirge. I spent 10 years teaching computer classes in college, and I like to think that I understand and sympathize with students as well as most, and it's for that very reason that I am very careful not to do a student's work for him or her. While it might help them receive a better grade than they deserve, it doesn't teach them a thing. What I am always happy to do is to explain how things work or give them insights when they are stuck, but never write code for them.

Re: help please

Posted: Wed Sep 16, 2009 7:22 am
by jackpf
peterjwest wrote:Here you are:

Code: Select all

Numbers
<table border="1">
  <tr>
    <th>od</th>
  </tr>
  <tr>
    <td>all the od numbers between 0 ro 1000, except the number 501</td>
  </tr>
</table>
If you'd like help, please at least try it first and post what you've managed so far. Also spend some time checking your spelling and grammar.

And you should know that 'odd' has two d's.
Awesome. :yar:

Re: help please

Posted: Wed Sep 16, 2009 9:18 am
by margaaa
wooooowww. well i will explain. i have my final exam of my course on thrusday, if i dont pass this exam i will have to stay all year doing only this discipline.
because of the mismatch of schedules and because i study and work i couldn´t go to the classes and now i just have some exemples of tests and i dont have where to study.
there was a guy in my college that said that would help me with this but now he doesnt answer my calls, so i asked for this so that i can learn from somewhere.
i have to say that this area has nothing to do with what i want to do professionaly.

thank you very much for your attention
and im sorry...

Re: help please

Posted: Wed Sep 16, 2009 9:32 am
by jackpf
So wait...you've done a whole PHP course and you still can't write a 9-lined script?

I don't mean to be rude...but that's pretty shocking 8O



Also,
Please note that $x&1 performs better (read faster) than $x%2.
(This is because $x&1 performs a simple binary comparison, while $x%2 performs a division, giving you the remainder.)
I actually found that modulo is faster.
With this script:

Code: Select all

<?php
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 100000000; $i++)
    if($i % 2)
        false;
echo sprintf('Modulo took %s seconds', array_sum(explode(' ', microtime())) - $begin);
 
$begin = array_sum(explode(' ', microtime()));
for($i = 0; $i <= 100000000; $i++)
    if($i & 2)
        false;
echo sprintf('Bitwise and took %s seconds', array_sum(explode(' ', microtime())) - $begin);
?>
...i got these results:
Modulo took 8.0031170845032 seconds
Bitwise and took 8.1880991458893 seconds

Re: help please

Posted: Wed Sep 16, 2009 11:32 am
by margaaa
lol no! my course was (or is) audiovisual and multimedia. but what i really want is audiovisual, but this discipline was obligatory, and i couldn´t go to the classes.

thank you soooo much :)

Re: help please

Posted: Wed Sep 16, 2009 11:34 am
by Mirge
Hope you're prepared to explain the code if you have to then lol

Re: help please

Posted: Wed Sep 16, 2009 11:45 am
by ricehigh
jackpf: My original findings where actually from C++, so i decided to do the test again in PHP. I did 100 million iterations of bitwise comparison and modulo operations respectively. I found that bitwise comparison was sligtly faster than modulo operations, but with such little margin, that it might as well have been a coincidence. So my conclusion is actually that in PHP it doesn't matter wether you use bitwise comparison or modulo.

(Note: I've tested with both small and large numbers and the result remains the same)