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
fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Do you guys typically put a function that returns a collection of objects inside the class for the object it returns?

Something like:

php:
<?
class User {
   static public function getUsers() {  }
}
?>
Or should it be in its own class (GetUsersResult) or something?

Adbot
ADBOT LOVES YOU

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
I put that in my data layer class(es) (generally my database class), as well as any other methods that would be fetching or manupilating data (e.g., SaveUser($user)).

go play outside Skyler
Nov 7, 2005


I'm having a pickle here. Is it better, when working on a large dataset inside a MySQL database, to do more complicated queries to select only the data you need, or risk select more than is needed then filter them out with PHP?

I guess what I'm asking is how should I balance the load between MySQL and PHP processing? If I try to convert my code to use more SQL, I'm going to end up with a dozen JOINs and some other really abstract stuff. Not that I can't write it, no. I'm actually wondering if I'm not better off doing the complicated filtering with PHP.

MrMoo
Sep 14, 2000

Sir Davey posted:

I'm actually wondering if I'm not better off doing the complicated filtering with PHP.

It's the database job to process the data, PHP to stick it in a template. Also consider stored procedures for extra processing in the database.

Hammerite
Mar 9, 2007

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

surrealcatalyst posted:

Don't you want to be comparing the input crypted by the original salt against the encrypted password, rather than decrypt the password?

Yes, that's what I meant. Using the encrypted form of the original password as the salt works because it starts with the salt, i.e. its first 12 characters are the salt and PHP doesn't care if the salt is longer than it needs to be. At least that's my understanding of it.

KuruMonkey
Jul 23, 2004

fletcher posted:

Do you guys typically put a function that returns a collection of objects inside the class for the object it returns?

I would generally have a class "User" and another for "UserDB" or something.

i.e. one class for 'a user' and another that 'is' the store of all users.

To get specific, I generally would be using CodeIgniter, so I'd have a UserModel, that handled interfacing with the DB, or whatever store it might be. That class would have the function that returned a collection of users.

I would then have a class for a single user (which generally I would put in a helper, so I can use that abstraction outside CI if I want) and that generally has functions like render()

I'm a big fan of a render() function at the moment, as it lets me pass the User objects to the view and then...render them.

I do constantly question if thats 'good' MVC, but to me its neat; I can rely on consistant rendering of the markup for, say, an image, as long as I'm instanciating my image class (so I get alt tags etc)

At some point I intend to make the common classes of this type I use also have an HTML rendering option, but I seem to be all xhtml these days.

Anyway; I have wittered.

Short version; no, no generating collections of an object from an object of that type. (but not saying its 'wrong' either)

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
How do I convert a unix time stamp date to whatever the end user's time zone is?

I notice the time() returns the UTC or whatever standard universal time zone. I'd like it to modify itself on the fly to whatever time zone the end user is in.

And thus the "T" time zone parameter will correspond to whatever the end user's time zone is.

My reference: http://us3.php.net/date

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
You want date_default_timezone_set().

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss

supster posted:

You want date_default_timezone_set().

Awesome,

But how do I input the end user's time zone into that function? For now, I set it to always just display time as PST, but I'd like it to work for whatever the end user's time zone is.

Much appreciated

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

cannibustacap posted:

Awesome,

But how do I input the end user's time zone into that function? For now, I set it to always just display time as PST, but I'd like it to work for whatever the end user's time zone is.

Much appreciated

You'll either need to have the user input their timezone, or use Javascript to pick up the timezone offset from their computer settings, I think.

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss

Golbez posted:

You'll either need to have the user input their timezone, or use Javascript to pick up the timezone offset from their computer settings, I think.

Oh, so the user's time zone isn't in any of the server variables? or IP address?

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Nope. And IP addresses shouldn't really tell you what timezone someone is in unless you try and lookup where it's coming from. But even then, you'd probably get the wrong timezone because the internet is like that.



Do I need to unset any class variables in __destruct() or does it do that by itself?

chocojosh
Jun 9, 2007

D00D.
I'd like to include a file but first make some regular expression substitutions to that file before including it. I have a website where I mixed HTML code and PHP code in my views. I am trying to clean up the code, and a friend suggested that I replace:

<?getString("<key>");?>

with

~gs=<key>~


While this replacement is simple, it does let me get rid of php code, and it saves me a few characters (which can be significant when I have 50+ getString calls in a single file).

I have been able to store the file contents into a string, and do the replacements. However the problem then is that if I echo the string any php code doesn't get included. I've been able to replace ~gs=<key>~ with the result of calling getString(<key>), but there is other php code in my views that needs to get executed (thus doing echo is still not enough). I've tried eval but I'm hesitant to use it (security risks) and it is currently giving me an error.

Some googling also suggested using a "clean" input file and then having a step to process it into the proper format for deployment, but I'd prefer to avoid the extra complexity of having two sets of files.


Is there a better solution?

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

cannibustacap posted:

Oh, so the user's time zone isn't in any of the server variables? or IP address?

So far as I know, the only way to know the user's time zone is either for them or their computer to tell you (though a form or javascript [which will still require a form to submit what Javascript picks up, I think]), or for you to guess through their IP, which is far from 100% and will cause more confusion than using a default zone.

Emo.fm
Jan 2, 2007
Just started with PHP, trying to write a form handler that will take some input from a POST form and write it to a text file. I can do the whole fopen, fwrite, fclose etc kind of thing when there's no form involved, but as soon as I use a form to execute the handler (even using the exact same code), I get permission denied. I know this isn't strictly a PHP question, but is there something about executing from a form instead of accessing the .php file directly that affects my file permissions? How can I get around this?

my code:
code:
#!/usr/local/bin/php
<html>
<body>
<?

$file = fopen("currentsong.txt","a");
fwrite($file,$_POST["song"]);
fclose($file);

?>
</body>
</html>
the error messages:

quote:

Warning: fopen(currentsong.txt): failed to open stream: Permission denied in /home/test.php on line 6

Warning: fwrite(): supplied argument is not a valid stream resource in /home/test.php on line 7

Warning: fclose(): supplied argument is not a valid stream resource in /home/test.php on line 8

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

drcru posted:

Do I need to unset any class variables in __destruct() or does it do that by itself?
Unless I'm understanding your question wrong, no. That happens automatically.


chocojosh posted:

Is there a better solution?
Why are you doing this in the first place? Using PHP to display dynamic content in your views is perfectly normal and I don't really think you should go about this string replacement business because in the end it will just cause extra unecessary processing and complexity with no benefit.

That said, if you really want to do it you're looking for ob_start() and ob_get_clean(). You will start output buffering with ob_start(), include the file, and then save all the contents of the output buffer as a string into a variable with ob_get_clean(). Then you can do your string replacement with that variable and then simply print it.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

chocojosh posted:


While this replacement is simple, it does let me get rid of php code, and it saves me a few characters (which can be significant when I have 50+ getString calls in a single file).


Is there a better solution?

Yea, use Smarty.

chocojosh
Jun 9, 2007

D00D.
I've decided for the moment to not bother doing anything fancy to the update of the views. I don't really want to touch output buffering because I'm already using it (I believe because there was issues getting session to work properly with the way the site is structured), and I don't want any conflicts/bugs to happen. I've decided for now to use what I have (it works and it's not painful for me to make changes because I'm familiar with the site design; it's a one person project that needs a few days of work once a year).

I've been taught it's bad structure to be mixing in business logic with views. For example here's the code to make sure the proper radio button is selected on a load ($var will be retrieved from the database); my code has many other examples like this:

code:
	<option value="1" <?if($var == "1") echo "SELECTED";?>>
			<?=getString("none");?>
	</option>
	<option value="2" <?if($var == "2") echo "SELECTED";?>>
			<?=getString("small");?>
	</option>
	<option value="3" <?if($var == "3") echo "SELECTED";?>>
			<?=getString("small");?>
	</option>
I think though that using a proper templating system would be the best way to go. Learning Django is on my todo list and it would probably be best for me to remake this site in Django so that I can see how much easier it is to build/modify.

MrMoo
Sep 14, 2000

chocojosh posted:

I think though that using a proper templating system would be the best way to go.
PHP is a proper template system it all depends how you feed the data to the template, so for your example you could do this:
php:
<?
/* controller */
$data = array( array( value => "1", label => getString("none") ),
               array( value => "2", label => getString("small") ),
               array( value => "3", label => getString("small") )
              );
foreach ($data as &$datum) {
  $datum["selected"] = $datum["value"] == $var;
}
...
?>
php:
<?
/* view */
?>
<div>
<?
foreach ($data as &$datum) {
?>
<option value="<?=$datum["value"]?>"<?=$datum["selected"]?" selected":""?>>
  <?=$datum["label"]?>
</option>
<?
}
?>
</div>

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

MrMoo posted:

PHP is a proper template system

This is true, and I totally understand the arguments against Smarty saying its not necessary. It's just that it makes it more work to take little shortcuts and really forces you to abstract your views.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Emo.fm posted:

Just started with PHP, trying to write a form handler that will take some input from a POST form and write it to a text file. I can do the whole fopen, fwrite, fclose etc kind of thing when there's no form involved, but as soon as I use a form to execute the handler (even using the exact same code), I get permission denied. I know this isn't strictly a PHP question, but is there something about executing from a form instead of accessing the .php file directly that affects my file permissions? How can I get around this?

the error messages:

It's crapping out before it even gets to the POST bit, so it doesn't look like that's the problem. You are *sure* it works w/o the POST? It won't change if the page was just loaded, or if it was sent POST data.

Replace $_POST['song'] with "lolz" and see if you get the same error when you load the page.

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss

Golbez posted:

You'll either need to have the user input their timezone, or use Javascript to pick up the timezone offset from their computer settings, I think.

Okay well what is the most efficent way to have javascript pick up the user's timezone and feed it into the PHP date functions?

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

cannibustacap posted:

Okay well what is the most efficent way to have javascript pick up the user's timezone and feed it into the PHP date functions?

Thing is, I don't know if this can be automatic. The way Wikipedia does it is, there's a button the user clicks which tells (I'm guessing here) Javascript to find out the time offset, and then the user has to click 'submit'. Wikipedia needs this due to its very international audience; for a normal American website, you may be better off with a simple "Atlantic/Eastern/Central/Mountain/Pacific/Alaskan/Hawaiian" dropdown list.

Scuzzy
May 12, 2001

I <3 Ayanami,
omg anime fag

cannibustacap posted:

Okay well what is the most efficent way to have javascript pick up the user's timezone and feed it into the PHP date functions?
I've had success with populating a hidden field with the value from the javascript method of Date "getTimezoneOffset()" (difference in minutes).

Eg: +10 GMT is going to be -600 (Provided they have the correct timezone setting on their OS)

Scuzzy fucked around with this message at 00:58 on Dec 22, 2008

duck monster
Dec 15, 2004

This
code:
  function num_rows() {
   // return count($this->Query_ID->fetchAll()); //Always returns zero also yuck.
   //return mysql_num_rows($c); //old code
  }
Has me miffed. Anyone know the equivilent to mysql_num_rows in pdo? Unfortunatelyt we cant use the count(id) thing, because its an attempt at switching toolkit on 40K+ lines of code, so we have to use pdos version of num_rows, but for the life of me I cant find it documented, and it seems utterly clownshoes that they'd have one for updates but not selects.

But I'd be hosed if I know what it is.

duck monster
Dec 15, 2004

duck monster posted:

This
code:
  function num_rows() {
   // return count($this->Query_ID->fetchAll()); //Always returns zero also yuck.
   //return mysql_num_rows($c); //old code
  }
Has me miffed. Anyone know the equivilent to mysql_num_rows in pdo? Unfortunatelyt we cant use the count(id) thing, because its an attempt at switching toolkit on 40K+ lines of code, so we have to use pdos version of num_rows, but for the life of me I cant find it documented, and it seems utterly clownshoes that they'd have one for updates but not selects.

But I'd be hosed if I know what it is.

Possibly solved my own question
code:
  function num_rows() {
  	$this->cQuery_ID = $this->dbh->query('SELECT FOUND_ROWS()');
  	return $this->cQuery_ID->fetchColumn(0);
  }
This seems to actually work, but if someone has a reason why this is a terrible solution, I'd love to hear it.

jasonbar
Apr 30, 2005
Apr 29, 2005

cannibustacap posted:

Okay well what is the most efficent way to have javascript pick up the user's timezone and feed it into the PHP date functions?

If you are storing information about the user, perhaps you can require them to pick a timezone at some point? If not, you could just use javascript to get their timezone, throw it into a hidden field and read it on the back end into session data.

If neither of those work for your situation, I bet you could send the time in the server's local time to the client, and convert it to the user's local time by modifying this to fit.

IndieRockLance
Jan 29, 2003

The devourer of worlds demands a Moon Pie to satiate his hunger!
I've been trial-and-erroring my way though modifying a Wordpress template and I've run into a problem that I can't figure out. On the archives page, I'm having it generate thumbnails from the posts, overlay a transparent PNG and overlay the post titles over those matte images. Here's my code from the archives.php page (breaks added for clarity):

code:
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<div style="position:relative; width:310px; height:150px;">
<img style="background:url(<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>)" 
src="http://www.newcenturyjournalism.com/tempblog/wp-content/themes/f8-lite/images/ThumbMatte.png" alt="" /></a>
<div style="position:absolute; top:115px; left:0.5em; width:310px; font-weight: bold; font-family: Arial; font-size:24px; color: #ffffff; text-transform:uppercase;">
<p><?php the_title() ?></p></div></div>
<?php } ?>
<p class="postmetadata"><?php the_time('M d, Y') ?> | <?php if(function_exists('the_ratings')) { the_ratings(); } ?></p></div>
I've got everything lined up and working but the problem is that the text field bleeds over too much on the bottom. Below the image, I have the date of the post and an AJAX ratings plug-in. But, because of the bleed, you can't click any of the star buttons.

I'm tempted to kill the left margin on the text and pretend like the bleed is on purpose, but I figure I may as well try to do it right first before I chump out.

Edit: I managed to shrink the line height enough to make the stars work they way they're formatted now, although ideally I'd rather have them on the same line as the date. I added "line-height:0%" to the text style to do that. But I still have some covering up the line immediately after the image.

IndieRockLance fucked around with this message at 06:39 on Dec 22, 2008

Mackerel, the Thief
Sep 24, 2003

IndieRockLance posted:

stuff.

That's some messy, messy code there.

For one thing, your anchor tag doesn't follow XML specification. It should be like this:

code:
<div style="position:relative...">
<a ...>
<img ...>
</a>
<div style="position:absolute...">
<?php the_title(); ?>...
</div>
In addition, I've never heard of someone applying a background url to an img tag. Apply it to a div.

KuruMonkey
Jul 23, 2004

Mackerel, the Thief posted:

In addition, I've never heard of someone applying a background url to an img tag. Apply it to a div.

Its perfectly valid CSS though, so it should be OK. (can't say for sure if browsers will do it properly...)

Agree with the messy code though; use CSS from an external file rather than string-concatenating a style attribute together; much more stylish!

KuruMonkey fucked around with this message at 21:49 on Dec 22, 2008

IndieRockLance
Jan 29, 2003

The devourer of worlds demands a Moon Pie to satiate his hunger!

KuruMonkey posted:

Its perfectly valid CSS though, so it should be OK. (can't say for sure if browsers will do it properly...)

Agree with the messy code though; use CSS from an external file rather than string-concatenating a style attribute together; much more stylish!

Yeah, it works. It's not pretty, but it works. I changed it to apply to the div though, just to be on the safe side (I also stuffed all that style data into the CSS file like I should have).

I think I solved my problem by changing the last p to a div. I'm sure there are more elegant solutions but it works and I can busy myself with cleaning up my mess after everything's functioning.

sonic bed head
Dec 18, 2003

this is naturual, baby!
I have a php project that is using cheap shared web hosting and developed directly on the source code because for the life of me I cannot get XAMPP's mail to work properly with the web hosting's SMTP server.

The project is becoming much bigger than originally anticipated and I need to get my source control on. It's no problem to install SVN on my local machine and check into it from my local workspace but I don't know how to correctly have it match up with my web hosting. Is there any way that I can synchronize directories over FTP? Is there any better way that I should be planning on doing this? Thanks.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

sonic bed head posted:

The project is becoming much bigger than originally anticipated and I need to get my source control on. It's no problem to install SVN on my local machine and check into it from my local workspace but I don't know how to correctly have it match up with my web hosting. Is there any way that I can synchronize directories over FTP? Is there any better way that I should be planning on doing this? Thanks.

Sounds like you want a build script that will take things out of SVN and put them on the server. I have a development build and then I copy files I have updated from that to my local machine, check them into SVN, and then from the server do something like: ./build-production-website.sh

I never manually modify the files in the production build, only my build script modifies things in production.

The main purpose of the script is to do an svn export but you can have it do all kinds of useful things like setting permissions, running database update scripts, etc that are needed to do a build.

Atom
Apr 6, 2005

by Y Kant Ozma Post

fletcher posted:

Do you guys typically put a function that returns a collection of objects inside the class for the object it returns?

Or should it be in its own class (GetUsersResult) or something?

I do this and it works just fine for me. Makes the most sense that a function related to a certain functionality should be in the namespace of the corresponding data structure. I have no problem calling user::get_users() unless I'd be doing it some non-static way like $search_engine->get_users();

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


KuruMonkey posted:

Its perfectly valid CSS though, so it should be OK. (can't say for sure if browsers will do it properly...)

Firefox will. I use a swirling thingy as the background image of a thumbnail generator since the thumbnails can take several seconds before they're ready.

grumm3t
Jul 1, 2004
k
Is there a better way to parse a string for certain words? Something like search string for any of these array keys and if you find one do something with the value of that key.

Description = Cisco IOS Software, C2960 Software (C2960-LANBASE-M), Version 12.2(35)S

code:
protected function setDeviceType() {
                $deviceDescription = $this->getDescription();

                if(strstr($deviceDescription, 'C2960-LANBASE-M')) {
                        $this->deviceType = 'Switch';
                        $this->deviceModel = 'C2960-LANBASE-M';
                }

                if(strstr($deviceDescription, 'C2960-LANBASEK9-M')) {
                        $this->deviceType = 'Switch';
                        $this->deviceModel = 'C2960-LANBASEK9-M';
                }

                if(strstr($deviceDescription, 'ServerIron Switch')) {
                        $this->deviceType = 'SLB';
                }

                if(strstr($deviceDescription, 'Cisco Adaptive Security Appliance')) {
                        $this->deviceType = 'ASA';
                }
        }

cannibustacap
Jul 7, 2003

Brrrruuuuuiinnssss
Hey I want to make a list of links where only the selected link is bold, the rest are normal.

Just like here http://www.amazon.com/gp/bestsellers/electronics/1036922/ref=pd_ts_e_nav Notice how DVD Players is bold and the rest are normal?


How would I, either in PHP or CSS, make that happen?

Only registered members can see post attachments!

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

CSS AND PHP would work if you want. Just change the style for that one link and add some sort of check to see where you are on the site.

Internet Headache
May 14, 2007
php:
<?
?>
<ul id="item-list">
<?php
foreach($item_list as $item) {
  ?><li<?php

  if ($item == $selected_item) {
    ?> class="selected"<?php
  }
  ?>><a href="URL">ITEM TITLE</a></li><?php
}
?></ul><?php
?>
code:
ul#item-list {list-style:none; margin-left: .5em}
#item-list li.selected a {font-weight: bold; text-decoration:none}

Internet Headache fucked around with this message at 21:49 on Dec 24, 2008

Adbot
ADBOT LOVES YOU

sonic bed head
Dec 18, 2003

this is naturual, baby!

fletcher posted:

Sounds like you want a build script that will take things out of SVN and put them on the server. I have a development build and then I copy files I have updated from that to my local machine, check them into SVN, and then from the server do something like: ./build-production-website.sh

I never manually modify the files in the production build, only my build script modifies things in production.

The main purpose of the script is to do an svn export but you can have it do all kinds of useful things like setting permissions, running database update scripts, etc that are needed to do a build.

I would really like to do something like this but I don't really know how to achieve it on a windows machine with no ssh access to my web hosting. I can only use FTP. Can I write a .bat that can do this using windows native ftp CLI?

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