|
DholmbladRU posted:When I execute the fallowing code I get one column not seperate4d by ',' So what is it separated by?
|
# ? Oct 27, 2010 03:57 |
|
|
# ? May 16, 2024 23:18 |
|
epswing posted:So what is it separated by? Nothing, it would be like this. value1 value2 value3 I opened the csv file up in notepad++ if that matters.
|
# ? Oct 27, 2010 04:22 |
|
DholmbladRU posted:I am trying to get the basics of this fputcsv(). When I execute the fallowing code I get one column not seperate4d by ','. However I am trying to get a row seperated by ',' What is in $columnHeadArray ? This works: php:<? $data = array( array('hello', 'world'), array('more', 'data') ); $fh = fopen('out.csv', 'w'); if($fh) { foreach($data as $datum) fputcsv($fh, $datum); fclose($fh); } ?> code:
But mostly notice the structure of $data: an array containing arrays for each line. If your structure is just an array, you're asking for one value per line in your code. Edit: which may well mean you don't want a loop at all: one call to fputcsv per line you want to write, not per value to write. KuruMonkey fucked around with this message at 11:53 on Oct 27, 2010 |
# ? Oct 27, 2010 11:50 |
|
I'm going nuts over here with this little problem. A calendar I wrote is counting November 7th twice.code:
code:
|
# ? Oct 27, 2010 14:30 |
|
FamousThomas posted:I'm going nuts over here with this little problem. A calendar I wrote is counting November 7th twice. This year, daylight saving time ends in the United States on November 7. There are 25 hours in 11/07/10, so adding 24 doesn't take you to a full day.
|
# ? Oct 27, 2010 14:58 |
|
Lousy farmers. Thanks, buddy
|
# ? Oct 27, 2010 15:11 |
|
FamousThomas posted:I'm going nuts over here with this little problem. A calendar I wrote is counting November 7th twice. Don't increment time stamps by seconds if you want to get the right answers - use strtotime: php:<? $day = strtotime('Oct 6 2010'); echo date('c', $day); //2010-10-06T00:00:00-05:00 $day = strtotime('+1 day', $day); echo date('c', $day)r; //2010-10-07T00:00:00-05:00 $day = strtotime('+1 day', $day); echo date('c', $day); //2010-10-08T00:00:00-05:00 ?>
|
# ? Oct 27, 2010 16:30 |
|
KuruMonkey posted:What is in $columnHeadArray ? columnheadarray is like this php:<? $columnHeadArray[0] = 'Input Variable'; $columnHeadArray[1] = 'Amount'; $columnHeadArray[2] = 'Month/Butget Item'; $columnHeadArray[3] = 'August'; $columnHeadArray[4] = 'September'; $columnHeadArray[5] = 'October'; $columnHeadArray[6] = 'November';?>
|
# ? Oct 27, 2010 20:25 |
|
DholmbladRU posted:columnheadarray is like this And you want: Input Variable,Amount,Month/Budget Item,August,September,November as the line in the file? then you just want ONE call to fputcsv: php:<? fputcsv($fh, $columnHeaderArray); ?> An example that might parallel what you're doing: php:<? // headers: just the one array $headers = array('first_name', 'last_name', 'gender'); // records: one array per line, wrapped in an array of all the lines $records = array( array('Barney', 'Rubble', 'Male'), array('Betty', 'Rubble', 'Female'), array('Wilma', 'Flintstone', 'Female'), array('Fred', 'Flintstone', 'Male') ); $fh = fopen('flintstones.csv', 'w'); if($fh) { fputcsv($fh, $headers); // headers just the once foreach($records as $record) // then each record in turn { fputcsv($fh, $record); } fclose($fh); } ?> code:
KuruMonkey fucked around with this message at 11:49 on Oct 28, 2010 |
# ? Oct 28, 2010 11:41 |
|
I'm learning OOP PHP right now and I have a question about intiating classes inside another class. Is this considered a faux pas? Right now I have: php:<? class DatabaseControls{ private $db_host = ******* private $db_name = ******* private $db_user = ******* private $db_pass = ******* function db_connect(){ $connection = mysql_connect($this->db_host,$this->db_user,$this->db_pass); if(!$connection) { die ("Could not connect: " . mysql_error()); } $select_db = mysql_select_db($this->db_name, $connection); } function db_query($query){ $result = mysql_query($query) or die ("There was an error while running the query: " . mysql_error()); return $result; } }//end database controls class Authenticate { public $db; function __construct(){ $this->db = new DatabaseControls(); }//end constructor function checkUserExists(){ $result = $this->db->db_query("SELECT * FROM cake_users WHERE username='verkaufer'"); $num = $this->db->db_numRows($result); } }//end authenticate ?> The way it is executed is like so: php:<? require_once "auth.php"; $db = new DatabaseControls(); $db->db_connect(); $auth = new Authenticate(); $exists = $auth->checkUserExists(); ?>
|
# ? Oct 28, 2010 23:07 |
|
Yes, this is a bad way of doing things. The reason being it's harder to write unit tests because the constructor of Authenticate always requires a valid DatabaseControls object. The preferred way of doing this is called dependency injection.php:<? class Authenticate { private $db; public function __construct() { } public function attachDb(DatabaseControls $db) { $this->db = $db; return $this; } public function checkUserExists() { $db = $this->checkDb(); // rest of code here } private function checkDb() { if ( is_null($this->db) ) { throw new Exception('a valid DB object is not attached'); } return $this->db; } } ?> php:<? require_once 'PHPUnit/Framework.php'; class AuthenticateTest extends PHPUnit_Framework_TestCase { /** * @expectedException PHPUnit_Framework_Error */ public function testAttachDb_RequiresValidDbObject() { // You could optionally write a dataProvider that attempts to inject many data types $authenticate = new Authenticate; $authenticate->attachDb(NULL); } public function testAttachDb_DbIsAttached() { $authenticate = new Authenticate; $this->assertType('Authenticate', $authenticate->attachDb($this->getMock('DatabaseControls')); } /** * @expectedException Exception */ public function testCheckUserExists_RequiresDbObject() { $authenticate = new Authenticate; $authenticate->checkUserExists(); } // Rest of your tests here } ?> Edit: And stop with the stupid // end constructor, // end function(), // end if bullshit. Every modern editor can brace match so it's easy to find where they end. musclecoder fucked around with this message at 23:41 on Oct 28, 2010 |
# ? Oct 28, 2010 23:38 |
|
Yeah something like that. I am trying to do this code:
php:<? $records = array( $incomeArray, $yearlyExpArray, $semExpArray, $monthExpArray);?> I was thinking about doing this in a function then calling this function for each $records. Or does that makes no sense? php:<? function getArray($tempArray) { //add key and 2 empty values to begining of array foreach ($tempArray as $key => $value) { //add one value for each month to array } } ?>
|
# ? Oct 29, 2010 00:21 |
|
musclecoder posted:OOP stuff Excellent example musclecoder. Is there any reason though that you aren't doing injection on your constructor? I usually go the constructor approach and throw an exception right there instead of doing an assertion later on. As it is you are doing an assertion every time $db is used when you could be doing it once in the constructor. Secondly, if testing for null in the constructor, the exception thrown will point to where the constructor is called (where it really needs to be fixed) instead of being thrown where you are calling a different method, which could be thousands of lines away or in a completely different file.
|
# ? Oct 29, 2010 17:45 |
|
I'm having trouble with a PDO update. I have a comments counter that should update itself, but doesn't:php:<? $SQL = $db->prepare("update blogposts set blog_commentcount = ((select blog_commentcount from blogposts where blog_id = :blog)+1) where blog_id = :blog;"); $SQL->bindParam(":blog", $cleanvars[3]); $SQL->execute();?> Any suggestions?
|
# ? Oct 29, 2010 18:14 |
|
gwar3k1 posted:I'm using a MySQL database, and I know that the query (when replaced with a blog_id) pulls data from the database - I tried it in phpmyadmin. I don't get an error when the code gets to this point, but the comment count doesn't increment. The database user definitely has update and select permissions. Edit: looking at the query, why aren't you just doing incrementfield=incrementfield+1? Edit 2: Oh, guy below me said the same re: incrementfield, before I'd saved. Yay fucked around with this message at 18:33 on Oct 29, 2010 |
# ? Oct 29, 2010 18:31 |
|
Begby posted:Excellent example musclecoder. I don't like putting it in the constructor because it makes having to have a mock object each time you create the object in your tests. It's not a huge thing, and setUp() can handle creating the mock database object for you, but I prefer to attach it at a later point. For example, you're writing a test for a method of the class that doesn't require the database at all. No need to inject it into the constructor. Personal preference though. You're right, but I just extend the Exception class and have a custom exception that includes a trace. Just go back one more trace level and you have the method that called checkDb(). To gwar3k1, what is that? code:
|
# ? Oct 29, 2010 18:32 |
|
Thanks, getting rid of the inner select fixed it. I don't know why I thought it was necessary in the first place.
|
# ? Oct 29, 2010 18:37 |
|
I am trying to print out the fallowing. But for some reason the middle loop is only adding the key 1 time. code:
php:<? foreach($records as $record) // then each record in turn { foreach ($record as $key => $value) { $temp[1] = $key; $temp[2] = ''; $temp[3] = ''; $temp[4] = $value; $temp[5] = $key; for($i = 6; $i <= 17; ) { $temp[i] = $value; $i++; } fputcsv($file,$temp); echo $record; } } ?>
|
# ? Nov 1, 2010 22:21 |
|
DholmbladRU posted:
Moral of the story: Always develop with error and notice reporting turned on. (Though IMO I think that one should be handled as a warning rather than a notice) Golbez fucked around with this message at 22:30 on Nov 1, 2010 |
# ? Nov 1, 2010 22:25 |
|
moral of the story, look closely at your code.. should be $i, thanks. DholmbladRU fucked around with this message at 00:49 on Nov 2, 2010 |
# ? Nov 1, 2010 23:27 |
|
I'm taking currency form input. I want to make sure they don't do something crazy and give me fractional cents. What's an easy way to make sure that, if there's a decimal point, only allow two digits after it? For example, if they put in 25.011, I want to know that it's wrong. Right now I'm trying a simple regex (\.\d{3}) but was wondering if there were a more elegant way. I thought about comparing the input to number_format($str, 2), but that only works if I also pad the input with zeroes, and at that point it's getting more complex than a regex.
|
# ? Nov 2, 2010 22:19 |
|
Golbez posted:I'm taking currency form input. I want to make sure they don't do something crazy and give me fractional cents. What's an easy way to make sure that, if there's a decimal point, only allow two digits after it? For example, if they put in 25.011, I want to know that it's wrong. Right now I'm trying a simple regex (\.\d{3}) but was wondering if there were a more elegant way. I thought about comparing the input to number_format($str, 2), but that only works if I also pad the input with zeroes, and at that point it's getting more complex than a regex. Regexes are always fun. code:
|
# ? Nov 2, 2010 22:42 |
|
Doesn't round() do what you want?
|
# ? Nov 2, 2010 22:48 |
|
Or I could use the tiny regex I posted to make sure there aren't three digits after the decimal? I know using a regex is often not the simplest solution, which is why I was querying for a simpler, more elegant solution. That monster you posted would seem to whitelist, but all I really need is a blacklist: return !preg_match('/\.\d{3}/', $string) The explode and strlen on the period is a good idea, though. quote:Doesn't round() do what you want?
|
# ? Nov 2, 2010 22:55 |
|
I think you've got your solution but another option would be two input fields, limiting the second to maxlength of 2 and concatenate the decimal in code: [__].[__]
|
# ? Nov 2, 2010 23:04 |
gwar3k1 posted:I think you've got your solution but another option would be two input fields, limiting the second to maxlength of 2 and concatenate the decimal in code: [__].[__] I've never seen this used before, doesn't seem like a very good idea either tbh.
|
|
# ? Nov 3, 2010 00:00 |
|
Why regex? sprintf('%.2f', $value) should do exactly what you need it to do for this...code:
|
# ? Nov 3, 2010 00:13 |
|
Thanks, but it's padding the number with zeroes, which I don't want, because it then means I'd have to pad both the input and the output with zeroes in order to see if they differ, and then I'm comparing the result of this function to... the result of this function. It's not that I want to trim or round; I want to know and reject. So far the explode idea seems to work for what I need. If there were an easy one that would take this... code:
code:
php:<? function foo($s, $limit = 2) { switch (substr_count('.', $s)) { // No decimal, no problems case 0: return true; // One decimal, see how big the area to the right of it is case 1: $a = explode('.', $s); return !(strlen($a[1]) > $limit); // More than one decimal, big problems default: return false; } } ?> gwar3k1 posted:I think you've got your solution but another option would be two input fields, limiting the second to maxlength of 2 and concatenate the decimal in code: [__].[__]
|
# ? Nov 3, 2010 14:30 |
|
php:<? sprintf('%01.2f', '1.234');?>
|
# ? Nov 3, 2010 14:36 |
|
musclecoder posted:
But it also converts 1.2 to 1.20, meaning I can't compare input and output to see if there's a problem. I mean, I could then trim zeroes, but then for 1 to 1.00, I'd have to make sure to also trim the ., and at this point we're approaching annoying complexity again. And, it also doesn't warn me when there's more than one decimal in the string. This would run something like... php:<? function foo($s, $limit = 2) { $x = sprintf('%01.'.$limit.'f', $s); return ($x === $s || rtrim(ltrim($x, '0'), '0.') === $s); } ?> Golbez fucked around with this message at 14:49 on Nov 3, 2010 |
# ? Nov 3, 2010 14:38 |
|
Could you just do aphp:<? round(sprintf('%01.2f', '1.20'), 2);?> Then wrap a strval() to cast it back to a string.
|
# ? Nov 3, 2010 15:08 |
|
musclecoder posted:Could you just do a
|
# ? Nov 3, 2010 15:37 |
|
In an almost completely unrelated aside, it looks like there's no "greater/lesser than or identical" comparison operator? So, if I do $s <= 10, it will return true if $s is 10 or "10". There's no way to do $s <== 10, to force it to return true iff $s is 10, without adding || $s === 10. I guess this makes sense, since there's no way to force typing on the >/< side of the operator except through casting, but it was still a small surprise.
|
# ? Nov 3, 2010 16:16 |
|
This is driving me crazy. How do you use substr? It keeps outputting the entire variable string no matter what arguments I give it. This is the line I'm using: substr($thefile, 5); This does not make any changes to the var $thefile no matter what arguments I give it. What am I doing wrong? EDIT: For some reason changing it to $thefile = substr($thefile, 5); makes it work, but I don't understand why. revmoo fucked around with this message at 17:42 on Nov 3, 2010 |
# ? Nov 3, 2010 17:39 |
|
revmoo posted:EDIT: For some reason changing it to $thefile = substr($thefile, 5); makes it work, but I don't understand why. Because the function doesn't modify the string in place, it returns a copy of the part of the string you specify that you have to explicitly store or pass on to something else.
|
# ? Nov 3, 2010 17:53 |
|
revmoo posted:This is driving me crazy. How do you use substr? It keeps outputting the entire variable string no matter what arguments I give it. substr($thefile, 5) won't change anything itself, it will simply return the substring. Which is exactly why you need to assign it to the variable to get it to do anything. PHP manual posted:Returns the extracted part of string or FALSE on failure. Edit: To illustrate why it would be a bad thing for commands like that to edit their input... Example one: substr('test', 2) - How's it going to edit 'test'? Example two: What if you want to do other things with that string? You'd have to assign it to another variable in the first place. php:<? $s = 'Test string'; $sub = $s; substr($sub, 5); // Let's say this changes $sub to 'string' echo 'Dropping the first five characters of '.$s.' gives us '.$sub; // As opposed to... $s = 'Test string'; $sub = substr($s, 5); // Assigns 'string' to $sub echo 'Dropping the first five characters of '.$s.' gives us '.$sub; ?> Golbez fucked around with this message at 18:00 on Nov 3, 2010 |
# ? Nov 3, 2010 17:54 |
|
Makes sense, thanks for the lesson!
|
# ? Nov 3, 2010 19:23 |
|
e: wrong thread
|
# ? Nov 3, 2010 21:07 |
|
I have a pretty large page with multiple POST functions coming back to it as flow control. So from the main menu/default part of the script (contained in an else block at the bottom of the file), I have a quicksearch box that takes me to another part (contained in an elseif (isset($_POST['quicksearch'])) in the middle of the file). If they put in an invalid search, I want it to come back to that default section and notify the user. There are three ways of doing this: 1) Rejigger the file so that the elseif(isset($_POST['search'])) block and its associated procedures are contained inside the else block, so that any errors generated by said procedures can be displayed on that webpage. 2) Perversion 1: Use header() to send people back to the main menu (the only part that can be accessed without a $_POST, after all) with a parameter in the URL telling the page to print the URL. 3) Perversion 2: Use goto. When I found myself actually coding #2 I wanted to come here and find out if that was beyond the pale and if I should just suck it up and use goto. I'd really rather not rejigger the flow of the file. Edit: The universe wants me to be pure. I forgot that goto was only added in PHP 5.3 (which we don't run), and since this is happening in the middle of my script it's after headers, so header() won't work. Rejiggering it is! Golbez fucked around with this message at 22:06 on Nov 3, 2010 |
# ? Nov 3, 2010 21:07 |
|
|
# ? May 16, 2024 23:18 |
|
Is there any sane way to deal with XML-RPC? Any pointers/suggested reads would be appreciated.
|
# ? Nov 3, 2010 23:26 |