The Ultimate Site of Worms Armageddon

Other Things => Off Topic => Topic started by: Ray on February 29, 2012, 03:22 PM

Title: PHP expert needed!
Post by: Ray on February 29, 2012, 03:22 PM
Hey!

I wrote a neat little calendar script, only for displaying purpose - yet - and I have a small problem that I cannot seem to figure out by my own. Right now, the display of the days - according to PHP's documentation - start from Sunday by default. Now I'd need that to be Monday but I just cannot figure out a way to do it. I'll show you the script so you can see the code and also, take a look at how it looks:

http://pentzzsolt.web.elte.hu/index.php

Please help me, I f@#!ed around a lot with that for loop, but the result was always leaving empty rows or there were two Mondays after each other... I'd appreciate if you could help me! :(
Title: Re: PHP expert needed!
Post by: Statik on February 29, 2012, 03:36 PM
Maybe show your code? :)

Here is PHP date() function: http://php.net/manual/en/function.date.php
"N" stands for ISO-8601 numeric representation of the day of the week (1 - Monday, 7 - Sunday).

Also you may find JavaScript-way more cool: http://jqueryui.com/demos/datepicker/#inline ;)
Title: Re: PHP expert needed!
Post by: Ray on February 29, 2012, 03:41 PM
Ah yea I forgot to attach the code!

I won't use JavaScript for development, JavaScript and it's libraries should only be used to add more features to a 100% done and working, tested work. It can be turned off and users can miss out the navigation menu or a core function - like this calendar - and navigate away from the site, which we don't want to happen, obviously. :)

And yea, I know the date() function, didn't find what I needed. :-X
Title: Re: PHP expert needed!
Post by: THeDoGG on February 29, 2012, 03:56 PM
lol @ php !
Title: Re: PHP expert needed!
Post by: Ray on February 29, 2012, 04:03 PM
Quote from: THeDoGG on February 29, 2012, 03:56 PM
lol @ php !
Please, either contribute to the topic or bring your wasteful ideas of your Ruby joke somewhere else.
Title: Re: PHP expert needed!
Post by: darKz on February 29, 2012, 04:26 PM
lol @ ray !

Sorry, couldn't resist. :D <3
Title: Re: PHP expert needed!
Post by: Statik on February 29, 2012, 04:30 PM
It should be smth like this (attached).

Also I have to disagree about JavaScript. In modern applications there can be more JS code than PHP (Ajax, jQuery and other stuff), disabled JS in browser => gtfo xD

About your code:
1) you should add missing <td>'s and </tr> for the last week;
2) don't print html code with echo, better include php into html.

Quote from: Ray on February 29, 2012, 04:03 PMPlease, either contribute to the topic or bring your wasteful ideas of your Ruby joke somewhere else.

Ruby fans? xD
Title: Re: PHP expert needed!
Post by: Ray on February 29, 2012, 04:39 PM
Quote from: Statik on February 29, 2012, 04:30 PMAlso I have to disagree about JavaScript. In modern applications there can be more JS code than PHP (Ajax, jQuery and other stuff), disabled JS in browser => gtfo xD
Yes, there can be more, after every core feature and value of a website is reachable through plain HTML. And "gtfo" is the attitude why so many website developers lose customers.

Quote from: Statik on February 29, 2012, 04:30 PM2) don't print html code with echo, better include php into html.
Why?
Title: Re: PHP expert needed!
Post by: GreatProfe on February 29, 2012, 04:47 PM
i would like to help You Ray, but i am not a specialist in caledaries... Tbh i never needed to use it.

Anyway i wanna check ur code and make some tests here :)
Title: Re: PHP expert needed!
Post by: Statik on February 29, 2012, 05:08 PM
QuoteWhy?

Compare:


<div class="mm">
   <p class="barman"><strong><?php echo $barman['name']; ?></strong></p>
</div>


and

Quote
echo '
<div class="mm">
   <p class="barman"><strong>'.$barman['name'].'</strong></p>
</div>';

The first one looks more pretty, huh? :)
Title: Re: PHP expert needed!
Post by: Ray on February 29, 2012, 05:16 PM
Absolutely not. The only reason to use your method would be that if an error occours in the PHP script, you can more easily determine the source of the error. Otherwise, opening and closing <?php and ?> every single time when you want to output a variable is just such a huge pain in the ass, imagine a site like this, where basically every word is a variable, due to it's multilinguality.

Your code was working well by the way, thank you, I got what you modified. :)
Title: Re: PHP expert needed!
Post by: GreatProfe on February 29, 2012, 05:27 PM
Check if it is right:

http://www.cesarvastuk.com/indexray.php
Title: Re: PHP expert needed!
Post by: Statik on February 29, 2012, 05:32 PM
All the best PHP frameworks use the first method or short syntax (<?=$var?>) or some template engine ({%var%}). Second method fails when there are different quotes inside, each of them will require "\". If you still prefer this way, then I would recommend to use heredoc synax (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc).

Good luck! :D
Title: Re: PHP expert needed!
Post by: Ray on February 29, 2012, 05:41 PM
Yes Professor, that is what I needed, Statik's version is also working well, but thanks anyway.

Well Statik, so far I haven't ran into any project which I couldn't handle with the methods I use (except from a few minor stupidities like this one :D) but note I'm pretty much a beginner at this field.

Thanks for your help dude. :-*
Title: Re: PHP expert needed!
Post by: GreatProfe on February 29, 2012, 05:47 PM
ae gj Statik

TUS is protected agains the machine coz We have fcking pro php developers xD
Title: Re: PHP expert needed!
Post by: THeDoGG on February 29, 2012, 09:52 PM
damn where is the smite button ?
Title: Re: PHP expert needed!
Post by: Peja on February 29, 2012, 09:56 PM
Quote from: THeDoGG on February 29, 2012, 09:52 PM
damn where is the smite button ?

https://www.tus-wa.com/forums/karma/karma-system-2-0/675/
Title: Re: PHP expert needed!
Post by: philie on February 29, 2012, 10:52 PM
Quote from: THeDoGG on February 29, 2012, 09:52 PM
damn where is the smite button ?

;D :D
Title: Re: PHP expert needed!
Post by: TheKomodo on March 01, 2012, 07:52 AM
Lol, I thought this said PCP expert needed...
Title: Re: PHP expert needed!
Post by: MonkeyIsland on March 01, 2012, 08:32 AM
Quote from: Statik on February 29, 2012, 05:32 PM
All the best PHP frameworks use the first method or short syntax (<?=$var?>) or some template engine ({%var%}). Second method fails when there are different quotes inside, each of them will require "\". If you still prefer this way, then I would recommend to use heredoc synax (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc).

Interesting statement. Please link me to some of these "best PHP frameworks"?
Title: Re: PHP expert needed!
Post by: Statik on March 01, 2012, 08:56 AM
Quote from: MonkeyIsland on March 01, 2012, 08:32 AM
Interesting statement. Please link me to some of these "best PHP frameworks"?

Here are some pages from Yii/Symfony/Zend frameworks guides/books where you can see that:

http://symfony.com/doc/current/book/templating.html
http://www.yiiframework.com/doc/guide/1.1/en/form.view
http://www.yiiframework.com/doc/blog/1.1/en/comment.create
http://framework.zend.com/manual/en/learning.quickstart.create-layout.html
http://framework.zend.com/manual/en/zend.view.introduction.html

They all follow the MVC pattern (http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), means separating business logic and view, that's why all view-files look simple and even using <?php ... ?> doesn't make them hard to read.

Here is a link for Ray, maybe he will consider to switch to some framework instead of solving all tasks manually: http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html :) But Symfony 2 looks a bit overcomplicated to me, so I would recommend to take a look at Yii first.

Hope it helps! :)
Title: Re: PHP expert needed!
Post by: MonkeyIsland on March 01, 2012, 11:12 AM
First off, using php tags inside html is a matter of taste and the original pattern of coding. Personally I find it extremely ugly not to mention it's cumbersome in many cases. A long html page with tons of variables makes it insanely hard to read, causes a lot of redundant open/close php tags to begin with. I guess PHP isn't the very first language you learned right? It seems like you learned some other language like C before, then "adjusted" PHP knowledge onto it?

Secondly, frameworks! Almost every decent php application is kinda a framework because it would be impossible to manage as it grows. TUS uses a framework but a private one. While using a pubic framework could be a good practice, but it is not a prescription you can write for everyone. You shouldn't suggest a public framework to someone who is in early learning stages. It is like giving a book "how to read fast" to a kid who is learning how to read.

Quote from: Statik on February 29, 2012, 05:32 PM
Second method fails when there are different quotes inside, each of them will require "\".

That's not correct. Not each of them, only for the same quote used for defining the string.
Title: Re: PHP expert needed!
Post by: Statik on March 01, 2012, 11:29 AM
Frameworks are just sets of best web practices, I don't force Ray to start using them right now, but I think every web developer should know OOP, MVC, patterns and other smart words :D

About php tags... I'm kinda surprised you prefer to put everything inside echo, doesn't it make the code unreadable? Also I'm not sure IDEs support syntax highlighting in this case... I agree there cases when it's the prefered way, but usually there are much more HTML code than PHP (if logic and presentation are well separated).
Title: Re: PHP expert needed!
Post by: Ray on March 02, 2012, 06:44 AM
Every decent web developer should learn the tool - in this case, the language of PHP - they are working with very thoroughly, maybe then move on to a framework. I am still at the beginning steps of both programming and the PHP language, so I will try and code my own solutions, because that is the best practise.
Title: Re: PHP expert needed!
Post by: THeDoGG on March 03, 2012, 01:23 PM
Best practise, maybe .. But if so you should actually first learn C, then once you master it you should switch to PHP!

We already had this debate before Ray, I was telling that frameworks are a good way to save time and bootstrap projects :) Depends on your purpose, if you consider time as money and you are working for customers then frameworks and libs are just a good way.
If you code for pleasure and if your goal is to master the language, then doing everything from scratch could be something interesting..
But using some framework can also be a nice way to "learn from examples"

You want a calendar ?? You could use this http://jqueryui.com/demos/datepicker/#default
Or maybe you prefer to code it all by yourself :) After all Jquery is just a JS framework, but why using it if you can code it all by yourself !!

I am not web developper, and I do not want to be one, but thanks to RoR I can deliver you a full customized dynamic website within few weeks (~ 1000 / 2000 €)
Title: Re: PHP expert needed!
Post by: Ray on March 07, 2012, 04:44 AM
If you code by yourself, you have a lot more freedom in what you do, and it's a great practise to be pushing yourself to be better at your job, which in this field is necessary.

It is a very basic accessibility question that you should not use something like JS for a core feature, like this calendar.
Title: Re: PHP expert needed!
Post by: Ray on March 11, 2012, 05:11 PM
Okay you lovely geeks, I could not find an answer for this - most likely due to my bad googleing skills - so help me out:

When appending values to an array and defining keys for it, is it possible to use any built-in functions to define those keys without using temporary variables?

while ($Row = mysql_fetch_assoc($Events))
{
$Year = date("Y", $Row['Date']);
$Month = date("n", $Row['Date']);
$Day = date("j", $Row['Date']);
$Time = date("G:i", $Row['Date']);
$EventList[$Year][$Month][$Day] = array
(
"Time" => $Time,
"City" => $Row['City'],
"Place" => $Row['Place'],
"Website" => $Row['Website']
);
}
mysql_free_result($Events);
unset($Year, $Month, $Day, $Time);


$Events contains a selection from the database and the Date records are formatted in Unix timestamp - yea MonkeyIsland, I am following your advice of keeping data as raw as possible :) - and this way, I need four temporary variables, which I don't like (well, actually only three, $Time is not necessary).

So is there any way to define these like this:

$EventList[date("Y", $Row['Date']]...
Title: Re: PHP expert needed!
Post by: MonkeyIsland on March 11, 2012, 05:35 PM
Don't you try before asking? :p


while ($Row = mysql_fetch_assoc($Events))
$EventList[date("Y", $Row['Date'])][date("n", $Row['Date'])][date("j", $Row['Date'])] = array(
"Time" => date("G:i", $Row['Date']),
"City" => $Row['City'],
"Place" => $Row['Place'],
"Website" => $Row['Website']
);
mysql_free_result($Events);
Title: Re: PHP expert needed!
Post by: Ray on March 11, 2012, 05:49 PM
I did, I just used the wrong syntax apparently. :) Thank you!
Title: Re: PHP expert needed!
Post by: Ray on March 29, 2012, 01:58 PM
Hey, here's an odd question:

As this website I'm finishing offers the same document in multiple languages - English and Hungarian - it's a good practise to have the alternative documents tagged in the <head> part, and I would like to do this, as Google apparently does not index the Hungarian version of the website.

That is because - as MonkeyIsland does here - I use separate english.php and hungarian.php files that contain the exact same array, filled with the English and Hungarian versions of the website's content. When the user selects language, the selection is stored in a session variable and php includes the selected php file with the array.

But... as this is a user interaction, search engines are unable to crawl that deep. So the question is: is it possible to tag an alternative version in the <head> section this way?

I was thinking of using the get method to send the language selector form's data to the server, which sounds the most sensible solution.

<link rel="alternate" hreflang="hu" href="http://url.com/?lang=hu" />

But the whole point of this method is to completely get rid of such parameters in the url, so maybe this could help?

if (isset($_GET['lang']))
    {
    $_SESSION['Language'] = $_GET['lang'];
    unset ($_GET['lang']);
    header ('Location: ' . $document_root . '/' . (isset($_GET['page'])) ? '?page=' . $_GET['page'] : ''))
    }


That way the user input does get stored in the session AND we also get rid of the url. Bare in mind, I only used get method here to be able to send input with a static url.

Any better ideas?
Title: Re: PHP expert needed!
Post by: Statik on March 29, 2012, 02:15 PM
If you wanna make your site really SEO friendly, then you can use subdomains (hu.site.com) or simulate subdirectories (site.com/hu) using Apache's mod_rewrite (if you are running this web server). Internationalization isn't a simple task, because there are many things which can be different (time format, money, etc), but (ye, again :D) frameworks allow to solve it with minimum effort and you can learn how to do that in a good way by reading their source code :)
Title: Re: PHP expert needed!
Post by: Ray on March 29, 2012, 02:57 PM
www.celpont-music.com

Just change the language at the top left and see how the news change their date formats. ;) The website is not anywhere near broad enough to really need a framework, it's absolutely unnecessary. I will use those once my skills are on a level when I make huge projects, until then
Quote(ye, again :D)
I'll keep practising the coding.

I am familiar with mod-rewrite - as much as anyone can be, what a pain in the ass .htaccess is, jesus... - and I do use it for friendly urls, such as domain.com/download/something.zip -> domain.com/download.php?file=something.zip, you get what I'm saying. But the idea is: like tus-wa.com, you change the language, but the url does not change.

So back to my previous post: any better ideas? :)
Title: Re: PHP expert needed!
Post by: Statik on March 29, 2012, 03:05 PM
Better idea than sessions? Cookies then! A session will be "destroyed" when you'll close your browser, a user will need to select language again and again... But as u said, it's not seo-friendly way. I mean it's a good idea to save user language, but translated pages should be also accessable via site.com/language_id/page_id or smth like that.
Title: Re: PHP expert needed!
Post by: Ray on March 29, 2012, 03:22 PM
Yea, the use of cookies is a good idea, but I think you didn't get what my question was. Anyway, I'm going with my idea on this one.
Title: Re: PHP expert needed!
Post by: Ray on April 30, 2012, 12:59 PM
I love this topic. :D

How do you manually send out 404 message to a browser? On this website, I use only the index.php file, every single sub-page is inside that, like this: celpont-music.com/index.php?1=calendar

Now, if you are given this url: celpont-music.com/index.php?1=calendarrrr the page does not generate 404, even though that is not actually a page.

I tried the following code in the top line:

$pages = array('', 'home', 'calendar', 'biography', 'discography', 'gallery');
if (isset($_GET[1]) && !in_array($_GET[1], $pages)) header('HTTP/1.0 404 Not Found');


Apparently, it does not work, since it loads the page instead of sending the user to the 404 error page. :-X
Title: Re: PHP expert needed!
Post by: Statik on April 30, 2012, 01:12 PM
header('HTTP/1.0 404 Not Found') doesn't automatically show the error page. It just sends headers... Probably you want smth like:


header('HTTP/1.0 404 Not Found');
die('ZOMG THE ERROR!!!');


:D
Title: Re: PHP expert needed!
Post by: Ray on April 30, 2012, 01:20 PM
I love how you never understand my questions. :D

I want to achieve that the web server actually understands that by giving wrong values to $_GET[1] it should respond with a 404. But since the web server understands the index.php file to be it's resource, that file can be served and it returns with a 200 response.

That of course leads to empty pages being able to be indexed and all kinds of mess. I need a workaround for this.
Title: Re: PHP expert needed!
Post by: MonkeyIsland on April 30, 2012, 02:24 PM
Do you want an error page similar to the 404 error your webserver is giving? This error (http://celpont-music.com/index2.php) you mean?

If so, here are 2 ways to achieve that:
1. Define all your pages in .htaccess file, then if that didn't qualify, htaccess automatically tell web server to show 404 error.
2. Trick your php to act like your webserver 404. For example this is the HTML code of your web server 404 page:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DotRoll Kft. </title>
<link rel="stylesheet" href="/http-error/style.css" type="text/css"/>
</head>

<body>

<div class="upper"></div>
<div class="empty"><img src="/http-error/img/logo.jpg" width="329" height="89" /></div>
    <div class="middle"><div class="info"><img src="/http-error/img/404.jpg" width="120" height="75" alt="404error" /></div>
    <div class="info">
        <p><strong>&nbsp;&nbsp;Az oldal nem található | Not found</strong></p>
       
      </div>
    </div>
    <div class="empty"><p><br /></p><a href="/">Vissza a főoldalra</a></div>
    <div class="footer"></div>
   
   
</body>
</html>


Now as Statik said, include the header and let the script die on this html:


header('HTTP/1.0 404 Not Found');
echo {desired htrml};
exit;
Title: Re: PHP expert needed!
Post by: Statik on April 30, 2012, 02:28 PM
You just don't understand me Ray, I clearly see what u mean. HTTP headers are just few lines in the HTTP response. You can send 404 Not Found and render the whole page, browser will display it, but search engines will ignore.
Title: Re: PHP expert needed!
Post by: Ray on April 30, 2012, 04:13 PM
Quote from: MonkeyIsland on April 30, 2012, 02:24 PMDefine all your pages in .htaccess file, then if that didn't qualify, htaccess automatically tell web server to show 404 error.
That's what I need to do I believe. I will get rid of that ugly error page the webserver shows now, I'll have a custom one, the point is to avoid this (http://www.celpont-music.com/index.php?1=biographys) from happening.

I didn't know that pages can be defined in .htaccess... I googled it, nothing found. What do you mean by that?

Quote from: Statik on April 30, 2012, 02:28 PMYou can send 404 Not Found and render the whole page, browser will display it, but search engines will ignore.
Browsers should not render a non-existing page in my opinion, that is a strange behaviour.

Whenever a user navigates on a wrong link like this (http://www.celpont-music.com/index.php?1=biographys) the webserver should give a 404 response and thus navigate the user away to the error page (this (http://celpont-music.com/index2.php) at the moment).
Title: Re: PHP expert needed!
Post by: Statik on April 30, 2012, 04:21 PM
Good error page = user-friendly page. There should be site menu on it and maybe some tips. I can't agree browser shouldn't render non-existing page. Also you always try to find smth wrong in my words, but I'm explaining you the basics. Good luck, hope MI will help you more :P
Title: Re: PHP expert needed!
Post by: MonkeyIsland on April 30, 2012, 06:42 PM
Quote from: Ray on April 30, 2012, 04:13 PM
Whenever a user navigates on a wrong link like this (http://www.celpont-music.com/index.php?1=biographys) the webserver should give a 404 response and thus navigate the user away to the error page (this (http://celpont-music.com/index2.php) at the moment).

How the webserver must recognize that celpont-music.com/index.php?1=biographys is not a valid url? If you insist on making the webserver that way, then htaccess is the way to go (if using Apache).
In this way, you gotta define your website pages inside htaccess, for example:


RedirectMatch 404 !^index.php?1=(home|biography|calendar)$


As you can see that only allows 3 pages. Each time you create a new page, you gotta edit your .htaccess to allow the new page.
That's why the php solution is better. When a page does not exist, with php you can show 404 error as I said in my previous post.
Title: Re: PHP expert needed!
Post by: Ray on May 01, 2012, 05:24 AM
Well, since the only generated pages are actually the gallery albums, this is exactly what I need. This is a nice way, thank you.

EDIT: too bad it doesn't seem to work. (http://www.celpont-music.com/index.php?1=gallerys) :(
Title: Re: PHP expert needed!
Post by: Ray on May 15, 2012, 04:11 PM
I have the following array:

Array
(
[0] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 500 )
[1] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 450 )
[2] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 670 )
[3] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 600 )
)


(printed with print_r())

I want to sort it by the offers, ascending, to look like this:

Array
(
[0] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 450 )
[1] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 500 )
[2] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 600 )
[3] => Array ( [firstname] => Firstname [lastname] => Lastname [email] => email@domain.com [offer] => 670 )
)


EXACTLY like this, it is very important to keep the ID's going from 0, 1... Any help would be appreciated! :-X
Title: Re: PHP expert needed!
Post by: Statik on May 15, 2012, 04:20 PM
http://php.net/manual/en/function.usort.php
Title: Re: PHP expert needed!
Post by: Ray on May 15, 2012, 05:03 PM
Cheerz! ;)
Title: Re: PHP expert needed!
Post by: MonkeyIsland on May 15, 2012, 05:42 PM
Damn wanted to play good cop bad cop with statk again. :/
Title: Re: PHP expert needed!
Post by: Rok on May 15, 2012, 06:46 PM
Finaly something from Statik that Ray was happy with! :)

Oh btw, we need a PHP forum. Make Ray the moderator.
Title: Re: PHP expert needed!
Post by: Statik on May 15, 2012, 06:56 PM
lol @ PHP forum :D
Title: Re: PHP expert needed!
Post by: Ray on May 15, 2012, 08:20 PM
I'm always happy that Statik helps me, I think we just suffer from that "neither of us are native English speakers" issue, so we never understand what the other really means.

That might be just my poor explanation and phrasing though.
Title: Re: PHP expert needed!
Post by: avirex on May 16, 2012, 10:21 AM
Lolyou guys both speak excellent english... Sometimes I wonder if statik is to pro for you ray?  Im mega newb,so I have no idea.. Just seems like statik always has an answer, and he seems leet :D
Title: Re: PHP expert needed!
Post by: Ray on May 16, 2012, 10:34 PM
I am completely clueless over validating 0 as integer. I am trying to validate user input in the range of 0 to 2. The user input is given to a function like this:

(int)mysql_real_escape_string(filter_var(trim($_POST['score']), FILTER_SANITIZE_NUMBER_INT))

Now inside the function, I even checked it to be exactly 0 like this:

if ($score === 0) echo 'something';

That worked. However... in the validation process, it's not working, probably because the FILTER_VALIDATE_INT filter does not validate 0 as an integer, even though I did everything for it to accept it as an integer. Values 1 and 2 are accepted by this:

filter_var($score, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0, 'max_range'=> 2 )))

Anyone can tell me why this validation fails for the value 0?

EDIT: found out 0 is handled as empty for some reason. :-X

Quote from: avirex on May 16, 2012, 10:21 AM
Lolyou guys both speak excellent english... Sometimes I wonder if statik is to pro for you ray?  Im mega newb,so I have no idea.. Just seems like statik always has an answer, and he seems leet :D
I'm far away from excellent English, but thank you, that feels nice from a native speaker. :-[ Yes, Statik always has an answer and he is a great coder, no doubt about that, I'm just usually looking for something else, not what he thinks and I meant that if I was Russian and I could ask him in our native language, he would give me the exact answer I'm looking for. :)
Title: Re: PHP expert needed!
Post by: Crazy on May 16, 2012, 11:46 PM
Get a life
Title: Re: PHP expert needed!
Post by: MonkeyIsland on May 17, 2012, 05:53 AM
Why so much trouble for a range Ray?


if (in_array(trim($_POST['score']), range(0, 2)))
   //do stuff.
Title: Re: PHP expert needed!
Post by: Statik on May 17, 2012, 08:16 AM
Hey MI, you are wrong (!), try to set $_POST['score'] to "2a", "a2", "bbb", etc. ;)

For Ray (less words, more links!):

http://php.net/manual/en/function.filter-var.php (NOTE: Returns the filtered data, or FALSE if the filter fails.)

Solution: http://www.cleancode.co.nz/blog/664/filter_validate_int-0
Title: Re: PHP expert needed!
Post by: Statik on May 17, 2012, 08:35 AM
Wasn't sure if I should write this... but simple regular expression is shorter and cleaner way to validate integer:


if (preg_match('/^[0-2]$/', $score))
    ...


:)
Title: Re: PHP expert needed!
Post by: Tomi on May 17, 2012, 10:30 AM
[OFF]
Interesting theme :D

Btw don't we have a C/C++ topic? Because I need help in Visual C++ xD
[/OFF]

Btw the website looks cool Ray! ;)
Title: Re: PHP expert needed!
Post by: MonkeyIsland on May 17, 2012, 10:32 AM
Statik he simply wants to validate the POST data to see whether it is one of the 3 values or not. I highly doubt if he wants to get "2" from "2a".
Title: Re: PHP expert needed!
Post by: Statik on May 17, 2012, 10:52 AM
Quote from: MonkeyIsland on May 17, 2012, 10:32 AM
Statik he simply wants to validate the POST data to see whether it is one of the 3 values or not. I highly doubt if he wants to get "2" from "2a".

Exactly, but when you use in_array() without the 3rd parameter set to true, the comparison is not strict, it means 2 == '2a' and 0 == 'abbbdd2'. Maybe smth like that will work tho: in_array($_POST['score'], array('0', '1', '2'), true), I didn't check.
Title: Re: PHP expert needed!
Post by: chakkman on May 17, 2012, 11:19 AM
Quote from: Crazy on May 16, 2012, 11:46 PM
Get a life

LOL

No offense, Ray. Was just a funny answer after all that code stuff. :)
Title: Re: PHP expert needed!
Post by: Ray on May 17, 2012, 07:23 PM
Actually, MonkeyIsland's method works, since I sanitize the integer first, check my post! :) Nice link Statik, I could not find that solution, my googleing skills suck seemingly. :D I'm gonna go with that, just to keep consistency at the maximum.

Isn't it strange that PHP automatically type casts int 0 to FALSE? ???

Quote from: chakkman on May 17, 2012, 11:19 AM
Quote from: Crazy on May 16, 2012, 11:46 PM
Get a life

LOL

No offense, Ray. Was just a funny answer after all that code stuff. :)
Non taken, was indeed funny. ;D
Title: Re: PHP expert needed!
Post by: Statik on May 17, 2012, 07:47 PM
Quote from: Ray on May 17, 2012, 07:23 PM
Isn't it strange that PHP automatically type casts int 0 to FALSE? ???

It would be strange if int 0 == TRUE :D It's a common practice, not only in PHP and you should remember about this thing :P
Title: Re: PHP expert needed!
Post by: nino on May 17, 2012, 08:06 PM
if statik = homo then  True , if Ray even kissed a girl then False , i hope them dont get mad at me(Array = king).

Thats the proprer code, any doubt contact me.
Title: Re: PHP expert needed!
Post by: Statik on May 17, 2012, 08:42 PM
nino, you don't belong there, SLAVE!
Title: Re: PHP expert needed!
Post by: nino on May 17, 2012, 08:49 PM
Quote from: Statik on May 17, 2012, 08:42 PM
nino, you don't belong there, SLAVE!

False xD