help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

margaaa
Forum Newbie
Posts: 4
Joined: Tue Sep 15, 2009 11:16 am

help please

Post 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
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: help please

Post 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.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: help please

Post 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.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: help please

Post 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
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: help please

Post 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.)
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: help please

Post 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.
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: help please

Post by ricehigh »

Mirge: If you read my post, you might have noticed that i agree with you :wink:
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: help please

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: help please

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: help please

Post 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:
margaaa
Forum Newbie
Posts: 4
Joined: Tue Sep 15, 2009 11:16 am

Re: help please

Post 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...
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: help please

Post 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
margaaa
Forum Newbie
Posts: 4
Joined: Tue Sep 15, 2009 11:16 am

Re: help please

Post 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 :)
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: help please

Post by Mirge »

Hope you're prepared to explain the code if you have to then lol
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: help please

Post 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)
Post Reply