Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
stoops
Jun 11, 2001
This is my array.
code:
Array
(
    [0] => Choose type...
    [1] => english
    [2] => science
    [3] => history
)
Is there any way i can get the array to look like this:

code:
Array
(
    [] => Choose type...
    [english] => english
    [science] => science
    [history] => history
)

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

stoops posted:

This is my array.
code:
Array
(
    [0] => Choose type...
    [1] => english
    [2] => science
    [3] => history
)
Is there any way i can get the array to look like this:

code:
Array
(
    [] => Choose type...
    [english] => english
    [science] => science
    [history] => history
)

Yes, although why do you want it to look like that?

PHP code:
$myarray = array (
    '' => 'Choose type...',
    'english' => 'english',
    'science' => 'science',
    'history' => 'history'
);

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

stoops posted:

This is my array.

Working specifically from what given, and assuming "0" will always end up being blank:

PHP code:
$output = array();
foreach ($array AS $key => $value)
{
    if (!$key) { $output[''] = $value; }
    else { $output[$value] = $value; }
}
I was surprised you can have a blank string ('') for an array key, but here we are.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

Golbez posted:

I was surprised you can have a blank string ('') for an array key, but here we are.

From the interactive prompt:

code:
php > $horror = array( '' => 'empty string', 0 => 'zero', null => 'null, lol');
php > 
php > 
php > print_r($horror);
Array
(
    [] => null, lol
    [0] => zero
)
Looks like the null got stringified. At least that's ... something approaching sane.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

McGlockenshire posted:

From the interactive prompt:

code:
php > $horror = array( '' => 'empty string', 0 => 'zero', null => 'null, lol');
php > 
php > 
php > print_r($horror);
Array
(
    [] => null, lol
    [0] => zero
)
Looks like the null got stringified. At least that's ... something approaching sane.

IIRC all array keys are strings. Even the default numeric keys are really the decimal string representations of the nonnegative integers.

revmoo
May 25, 2006

#basta
<?php
$options = array(
'english' => 'english',
etc...
);

?>

<select name="options">
<option value="0">Choose type...</option>
<?php foreach ($options as $o) { ?>
<option value="<?php echo $o; ?>"><?php echo $o; ?></option>
<?php } ?>
</select>

stoops
Jun 11, 2001

Golbez posted:

Working specifically from what given, and assuming "0" will always end up being blank:

PHP code:
$output = array();
foreach ($array AS $key => $value)
{
    if (!$key) { $output[''] = $value; }
    else { $output[$value] = $value; }
}
I was surprised you can have a blank string ('') for an array key, but here we are.

Thanks for this.

Hammerite, The reason I wanted to do it this way is because I'm getting the arra values from a database and i wanted to populate the select box with these values. The thing is, what gets submitted to the form is '0' or '1' or '2' etc, and I wanted to submit a more friendly string.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

Hammerite posted:

IIRC all array keys are strings. Even the default numeric keys are really the decimal string representations of the nonnegative integers.

Looks like it's the other way around - numeric strings are cast to integers. Can't say this makes me happy. The bit about floats being cast to ints ... hasn't bitten me in the past but that doesn't mean I don't like it.

Here's the documentation:

PHP.net posted:

Additionally the following key casts will occur:

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8.
Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0.
Null will be cast to the empty string, i.e. the key null will actually be stored under "".
Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type.

Golbez fucked around with this message at 19:18 on Sep 20, 2012

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
:holy: We're getting 5.3 this weekend! And MySQL 5.5! :holy:

I've been trapped in a 5.1 gulag for years. What massive stuff do I need to know about from 5.2 and 5.3? The short list I've come up with in the last 30 seconds is:
1) Array dereferencing, gently caress yeah.
2) Namespaces. Know nothing about them or why they're useful. What is the point?
3) Frameworks. Oh god, frameworks. I so very much want to move our site to a framework. But, being in 5.1, my only experience is with an old version of Zend. What is the current hotness?
4) InnoDB. This is MySQL, but it also means I have to change many many many of our queries to handle transactions. We're not converting from MyISAM immediately but it's something we'll be doing.
4a) mysqli or PDO. Oh gently caress me I've been wanting this for so long, it's not that I couldn't but I always wondered if there was any point without a framework. ... There probably is, actually, and I could just shoehorn one in, but eh. This point is more e/n than anything else.
5) I will be using the poo poo out of the datetime objects.

Anything else? I'm excited! :woop:

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.

Golbez posted:

:holy: We're getting 5.3 this weekend! And MySQL 5.5! :holy:

I've been trapped in a 5.1 gulag for years. What massive stuff do I need to know about from 5.2 and 5.3? The short list I've come up with in the last 30 seconds is:
1) Array dereferencing, gently caress yeah.
2) Namespaces. Know nothing about them or why they're useful. What is the point?
3) Frameworks. Oh god, frameworks. I so very much want to move our site to a framework. But, being in 5.1, my only experience is with an old version of Zend. What is the current hotness?
4) InnoDB. This is MySQL, but it also means I have to change many many many of our queries to handle transactions. We're not converting from MyISAM immediately but it's something we'll be doing.
4a) mysqli or PDO. Oh gently caress me I've been wanting this for so long, it's not that I couldn't but I always wondered if there was any point without a framework. ... There probably is, actually, and I could just shoehorn one in, but eh. This point is more e/n than anything else.
5) I will be using the poo poo out of the datetime objects.

Anything else? I'm excited! :woop:

1) That didn't happen until PHP5.4, so don't go using it yet.
2) I love them. Instead of having a long class named Zend_Db_Something_Or_Other_Factory, you could throw that in the Zend\Db\Something\Or\Other namespace, and your class would just be named Factory. Then to use it, you can do: use Zend\Db\Something\Or\Other\Factory; $f = new Factory;

You can also alias Factory to another name to avoid conflicts: use Zend\Db\Something\Or\Other\Factory as ZendFactory; $f = new ZendFactory.
3) Symfony2 and Zend2 just released new versions of their frameworks. I'm partial to Symfony2 personally but I have no experience with Zend2.
4) Good, hopefully you'll be using PDO (or an ORM which will use PDO).
4a) Always use PDO. "But mysqli is nice!" Nope, just use PDO.
5) DateTime is a great library.

I urge you to just upgrade to 5.4. That way you can get array dereferencing and you get the built in server which is awesome. No more setting up vhosts for small projects. Symfony2 has it built into their console:

php app/console server:run

Bam, go to http://localhost:8000/ and your Symfony2 app is running right there. So nice.

Edit: Oh yeah, 5.4 also has short JavaScript style arrays which are nice (but harder to grep for):

$a = ['a', 'b', 'c'];

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

musclecoder posted:

I urge you to just upgrade to 5.4. That way you can get array dereferencing and you get the built in server which is awesome. No more setting up vhosts for small projects. Symfony2 has it built into their console:

Can't upgrade to 5.4. We gotta stick with what crumbs Red Hat throws us, for whatever reason.

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.

Golbez posted:

Can't upgrade to 5.4. We gotta stick with what crumbs Red Hat throws us, for whatever reason.

Bummer :(. 5.3 at least is a huge step up from (I'm guessing 5.1.6 which is what shipped with RedHat/CentOS forever).

What version of 5.3? If you use any XML-RPC libraries, 5.3 earlier than 5.3.8 has a bug with them. That was the biggest (and only) thing that got me with 5.3.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

musclecoder posted:

Bummer :(. 5.3 at least is a huge step up from (I'm guessing 5.1.6 which is what shipped with RedHat/CentOS forever).

What version of 5.3? If you use any XML-RPC libraries, 5.3 earlier than 5.3.8 has a bug with them. That was the biggest (and only) thing that got me with 5.3.

Yeah, 5.1.6. It annoys me. As for which 5.3.x, I don't know, but knowing Red Hat they've probably stayed on top of that.

SimonNotGarfunkel
Jan 28, 2011
Man, reading this thread really makes me want to get my teeth back into a bit of PHP. The last time I really touched it was with the release of PHP 5 and the whole OOP thing.

I create apps in .Net at work but there's something about PHP which makes it much more enjoyable to code in.

Are there any must read articles I should look at to talk me through the major new features in the last few years? Or just feel free to throw some words at me and I'll look them up.

Also, this has probably been asked a million times but what's generally considered the best template system these days? Last thing I used was smarty.

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.
PHP has matured, but still has years and years of lovely history you have to put up with.

Most of the things that you find in PHP you find in other competent development teams:

* Automated testing of all types
* Builds with Phing
* Continuous integration
* Continuous deployment
* Database migrations
* Vendored code
* http://www.12factor.net/ is a great site by Heroku showing what a good web app is made up of.

Definitely not Smarty. I use Symfony2 which uses Twig and I find it enjoyable. One nice construct I like is the {% for %} {% else %} block:

code:
{% for var in list %}
    <strong>{{ var }}</strong><br>
{% else %}
    <strong>Sorry nothing to list!
{% endfor %}
That's always a construct I wish a language would have. PHP is still very verbose compared to Python and Ruby. And if you're gonna use a database, use Postgres. :)

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
Php is already a template language, especially with short tags & alternative syntax, why throw another one on top of it?

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.

Golbez posted:

:holy: We're getting 5.3 this weekend! And MySQL 5.5! :holy:

I've been trapped in a 5.1 gulag for years. What massive stuff do I need to know about from 5.2 and 5.3? The short list I've come up with in the last 30 seconds is:
1) Array dereferencing, gently caress yeah.
2) Namespaces. Know nothing about them or why they're useful. What is the point?
3) Frameworks. Oh god, frameworks. I so very much want to move our site to a framework. But, being in 5.1, my only experience is with an old version of Zend. What is the current hotness?
4) InnoDB. This is MySQL, but it also means I have to change many many many of our queries to handle transactions. We're not converting from MyISAM immediately but it's something we'll be doing.
4a) mysqli or PDO. Oh gently caress me I've been wanting this for so long, it's not that I couldn't but I always wondered if there was any point without a framework. ... There probably is, actually, and I could just shoehorn one in, but eh. This point is more e/n than anything else.
5) I will be using the poo poo out of the datetime objects.

Anything else? I'm excited! :woop:

4a) PDO is vastly better than MySQLi. That's my own experience, anyway.

6) Late static binding. gently caress yes. Objects get more useful!

Mister Chief
Jun 6, 2011

Is there any advantage to array dereferencing other than saving a line of code?

McGlockenshire
Dec 16, 2005

GOLLOCKS!
It saves a line of code and a temporary variable. It's not much, but it adds up. Consider it a quality of life / syntactic sugar thing.

SimonNotGarfunkel
Jan 28, 2011
Thanks musclecoder. That link should come in handy as I've picked up a lot of bad habits in my current place.

KARMA! posted:

Php is already a template language, especially with short tags & alternative syntax, why throw another one on top of it?

I just like to keep the back-end code and design separate from one and other. I find it a lot easier to develop like that I guess.

Impotence
Nov 8, 2010
Lipstick Apathy
Probably a quick dumb thing, but I forgot how to do this:

How would I push an empty array into another array, with the array's key named whatever is in (string) $p['intToken'], so I can later push something else into $teams[$p['intToken']]['whatever']?

Doh004
Apr 22, 2007

Mmmmm Donuts...

Biowarfare posted:

Probably a quick dumb thing, but I forgot how to do this:

How would I push an empty array into another array, with the array's key named whatever is in (string) $p['intToken'], so I can later push something else into $teams[$p['intToken']]['whatever']?

PHP code:
$teams[$p['token']] = array();
Then you can do $teams[$p['token']['hurfadurf'] = 'blerg';

Experto Crede
Aug 19, 2008

Keep on Truckin'
I'm pretty drat new to php, and I'm trying to wrap my head around php's read_exif_data function. As a test, I want it to read the data's "Make" tag and echo text depending on if the make is a certain one.

So far I have this:

php:
<?php
$image "IMG_20120922_125625.jpg";
$exif exif_read_data($image,"Make"0false);
if ($exif == "HTC"){
    echo "HTC";
}
else{
    echo "Not HTC";
}
?>

But regardless of the image (I have a test image from a samsung and htc camera), it just echoes "HTC". I know it's probably something really simple I'm doing wrong (Getting a bit confused by the info on the php manual), but I can't see it :downs:

Any advice?

Experto Crede fucked around with this message at 12:01 on Sep 23, 2012

SimonNotGarfunkel
Jan 28, 2011

Experto Crede posted:

I'm pretty drat new to php, and I'm trying to wrap my head around php's read_exif_data function. As a test, I want it to read the data's "Make" tag and echo text depending on if the make is a certain one.

So far I have this:

php:
<?php
$image "IMG_20120922_125625.jpg";
$exif exif_read_data($image,"Make"0false);
if ($exif == "HTC"){
    echo "HTC";
}
else{
    echo "Not HTC";
}
?>

But regardless of the image (I have a test image from a samsung and htc camera), it just echoes "HTC". I know it's probably something really simple I'm doing wrong (Getting a bit confused by the info on the php manual), but I can't see it :downs:

Any advice?

Is it something as simple as changing $exif == "HTC" to $exif === "HTC"?

I can't see anything else that might be wrong with it but I've never used the exif functions before.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Try printing out the contents of $exif.

-JS-
Jun 1, 2004

Jabor posted:

Try printing out the contents of $exif.

This. exif_read_data returns an array, or false if it can't find anything.

If you print_r($exif) you'll see how you need to reference the returned array to get what you want. (It'll probably be something like $exif['Make'])

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine

bobthecheese posted:

4a) PDO is vastly better than MySQLi. That's my own experience, anyway.

6) Late static binding. gently caress yes. Objects get more useful!

Yeah, PDO makes sense, especially since we are open to switching away from MySQL in the future.

As for late static binding, it seems useful, but my objects are so pedestrian and simple as to only barely inherit, let alone do anything complex enough for that. :sigh:

Oh, and speaking of sighs, guess what upgrade was put off a week? Cmon, guess. :smith:

SimonNotGarfunkel
Jan 28, 2011
I've been trying to get my head around Zend studio and the framework in general the last couple of days.

Apart from the manual, are their any decent articles to get me started? I'm still trying to get my head around the models/controllers/views stuff so need a beginners guide really.

McGlockenshire
Dec 16, 2005

GOLLOCKS!
If you're new to MVC, Zend Framework is the worst possible choice. Especially version 1.

Not that there's anything wrong with it, really. The problem is that ZF is a horribly complex beast that takes hints from Java's way of doing things. It's a bad place for beginners to start.

You don't need a framework to follow the principals of MVC.

SimonNotGarfunkel
Jan 28, 2011
Thanks man, it's certainly not as easy as I was expecting.

However, I've been casually looking at jobs and a lot of them require knowledge of Zend. Not all, but a lot so I thought it would give me an advantage.

Edit: I guess I'll look at MVC in general then and maybe move onto Zend in the future. Thanks for the advice.

McGlockenshire
Dec 16, 2005

GOLLOCKS!
You also want to stop using the word Zend without a qualifier.

Zend is a company. They produce an IDE (Zend Studio), an integrated server product (Zend Server), and sponsor but do not control the development of an open source library (Zend Framework). None of the three require the others, and knowing one does not teach you anything about the others. When talking about the framework, just "ZF" is enough to give context to most people that know it.

Pile Of Garbage
May 28, 2007



bobthecheese posted:

4a) PDO is vastly better than MySQLi. That's my own experience, anyway.

6) Late static binding. gently caress yes. Objects get more useful!

I currently rent a small VPS with Linode where I run a LAMP instance which I purely use to muck around with PHP. I was previously using mysqli however from your post I decided to look into PDO and god drat it beats the crap out of mysqli. I spent some time yesterday redoing code that I'd done to use PDO instead of mysqli and it is so much easier to work with (Prepared statements rock).

SimonNotGarfunkel
Jan 28, 2011

McGlockenshire posted:

You also want to stop using the word Zend without a qualifier.

Zend is a company. They produce an IDE (Zend Studio), an integrated server product (Zend Server), and sponsor but do not control the development of an open source library (Zend Framework). None of the three require the others, and knowing one does not teach you anything about the others. When talking about the framework, just "ZF" is enough to give context to most people that know it.

Got you.

Anyway, I played around with CodeIgniter last night and my god is it so much easier to pick up.

Deus Rex
Mar 5, 2005

SimonNotGarfunkel posted:

Got you.

Anyway, I played around with CodeIgniter last night and my god is it so much easier to pick up.

we're using FuelPHP which has its warts, but it meant to be a 'cleaner' version of CI or something. Anyway, I've poked around for fun at Laravel and it seems really very promising.

Null Set
Nov 5, 2007

the dog represents disdain

SimonNotGarfunkel posted:

Got you.

Anyway, I played around with CodeIgniter last night and my god is it so much easier to pick up.

Personally, I would avoid CI for the fact that they don't use parameterized statements in their database drivers, and instead rely on custom built sanitizing functions. Makes me wonder what other warts are lurking in it.

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.

SimonNotGarfunkel posted:

Got you.

Anyway, I played around with CodeIgniter last night and my god is it so much easier to pick up.

You might want to start with some of the Sinatra-esque frameworks like Silex to get a feel for MVC and then move on to one of the modern (PHP5.3+) ones.

putin is a cunt
Apr 5, 2007

BOY DO I SURE ENJOY TRASH. THERE'S NOTHING MORE I LOVE THAN TO SIT DOWN IN FRONT OF THE BIG SCREEN AND EAT A BIIIIG STEAMY BOWL OF SHIT. WARNER BROS CAN COME OVER TO MY HOUSE AND ASSFUCK MY MOM WHILE I WATCH AND I WOULD CERTIFY IT FRESH, NO QUESTION

Deus Rex posted:

we're using FuelPHP which has its warts, but it meant to be a 'cleaner' version of CI or something. Anyway, I've poked around for fun at Laravel and it seems really very promising.

Thanks for this - I hadn't heard of Laravel and it looks quite interesting.

Edit:

So I've been looking into Laravel, playing around with it a bit, and I'm struggling to get the hang of the Eloquent ORM. In my schema I have a USERS table and a TEAMS table, each user record has a TEAM_ID (which may be null) representing the football team they support.

I must be misunderstanding something, because I would call this a one to many (one team can be linked to any number of users, but each user will be linked to just one team). According to the docs I would reflect a one-to-many relationship in the code by saying:
PHP code:
function teams()
{
    return $this->has_one('team');
}
But when I do this it looks for a 'USER_ID' in the TEAMS table. I don't understand why it would do that - surely if I'm saying the USER model has_one TEAM it would imply a foreign key of TEAM_ID in the USER table.

I'm clearly misunderstanding something fairly fundamental, someone please help!

putin is a cunt fucked around with this message at 16:45 on Sep 26, 2012

PleasantDilemma
Dec 5, 2006

The Last Hope for Peace
Yeah there are a lot of frameworks out there. Which one has built in models to manage users? I'd like something that already has the sign up/login/recover password thing already made and I can just extend from. I've been using code igniter because I have experience with it from a school project, but it doesn't come with user stuff already.

putin is a cunt
Apr 5, 2007

BOY DO I SURE ENJOY TRASH. THERE'S NOTHING MORE I LOVE THAN TO SIT DOWN IN FRONT OF THE BIG SCREEN AND EAT A BIIIIG STEAMY BOWL OF SHIT. WARNER BROS CAN COME OVER TO MY HOUSE AND ASSFUCK MY MOM WHILE I WATCH AND I WOULD CERTIFY IT FRESH, NO QUESTION

PlesantDilemma posted:

Yeah there are a lot of frameworks out there. Which one has built in models to manage users? I'd like something that already has the sign up/login/recover password thing already made and I can just extend from. I've been using code igniter because I have experience with it from a school project, but it doesn't come with user stuff already.

Laravel comes with the Auth stuff built-in, it seems like it's going to be the next big thing in my opinion as it's incredibly well made and actually makes good use of the improved OO in PHP 5.3

Edit: forgot to mention, also has great documentation.

Adbot
ADBOT LOVES YOU

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
How recommended/good is it to shoehorn an existing site (and more importantly, an existing database schema) into a framework?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply