|
Jesus, okay, so using musclecoder's method for figuring out how much memory I'm using, a 5-ish mb csv file is using 72mb. Jesus gently caress. I'll do it this way and use it as ammo to get MySQL up and running sooner rather then later. I hate that our internal machines are are so much slower then any of our public facing poo poo. It really is a kick in the teeth when they ask you to do something absurd like this. Thanks!
|
# ? Apr 12, 2011 23:22 |
|
|
# ? Jun 8, 2024 00:21 |
|
God I loathe having to work on poo poo like this with php...new question time! I have my array being built with a function, it reads the first row for the keys, then uses each subsequent row of the CSV as the value. So, for example, my CSV is like so: code:
code:
code:
Otherwise, I'm going to do it this way instead, which I don't like quite as much: code:
Ideas on how I could be doing this better? Winkle-Daddy fucked around with this message at 23:17 on Apr 13, 2011 |
# ? Apr 13, 2011 21:58 |
|
Winkle-Daddy posted:Ideas on how I could be doing this better? what version if PHP are you running? php:<? function csv_match_tuple($path, $key_index, $key_value) { $found = FALSE; $fh = fopen($path, 'r'); if($fh) { while(!feof($fh) && $found === FALSE) { $tuple = fgetcsv($fh); if(array_key_exists($key_index, $tuple) && $tuple[$key_index] == $key_value) { $found = TRUE; } } fclose($fh); } return ($found === TRUE ? $tuple : NULL); } ?> Edit; be aware I've not actually run that code; treat it as a statement of intent rather than usable code Edit2: are you trying to load the whole CSV into an array and then search? If so; thats the cause of your memory error - scan it line by line. Unless each line of your csv is huge, then line by line should NOT use much memory at all. KuruMonkey fucked around with this message at 22:35 on Apr 13, 2011 |
# ? Apr 13, 2011 22:27 |
|
Thanks, I ended up doing something similar that I was able to bastardize off of php.net since I already have my code loading up the CSV into an array. It's just a quick little function to search the multi-dimensional array and returns a true/false for the key value. I know this is awfully inefficient but I got confirmation this morning that I will eventually be able to put this into a mysql DB...I just need something that works until then...so the project manager will be getting this total poo poo storm until they get my DB set up proper. gently caress release cycles.
|
# ? Apr 13, 2011 22:44 |
|
Nichololas posted:I debated briefly but figured this thread would be more appropriate than the Java questions that don't need their own thread.. I've used it before, though I wasn't the one to set it up, which I understood was a pain to get just right. The biggest problem is that it leaves zombie Java processes as Apache children. You will need to periodically (daily cron job) kill them off manually, or you'll run out of memory. You might want to consider another solution, like a cross-platform work queue. I like Gearman. It seems like I'm constantly plugging it, but it's a great tool with support in pretty much every language and platform.
|
# ? Apr 14, 2011 05:33 |
|
musclecoder posted:Quick and dirty:
|
# ? Apr 15, 2011 01:17 |
|
I've got a question, the code is Zend Framework but it is essentially just a MVC (or simply an OO) question. What's the best practice when it comes putting code in the constructor to create an object, particularly doing a DB call. For example, which of these two snippets is the better way to do it (and if you are feeling friendly, why?) php:<? class Application_Model_Person { protected $_person_id; protected $_name; function __construct($person_id){ $this->_person_id= $person_id; } public function setName($name){ $this->_name = $name; } public function getName() { return $this->_name; } public function hello(){ return "Sup yo, I'm " . $this->_name; } } ... class PersonController extends Zend_Controller_Action { public function PersonAction(){ $person_id = ...->getParam('pid'); // ASSUME INSTANTIATE DB CODE GOES HERE $person_data = ...->select("person_id = {$person_id}"); $speaker = new Application_Model_Person($person_id); $speaker->setName($personData->name); $this->view->greeting = $speaker->hello(); } } ?> php:<? class Application_Model_Person { protected $_person_id; protected $_name; function __construct($person_id){ $this->_person_id= $person_id; // ASSUME INSTANTIATE DB CODE GOES HERE $person_data = ...->select("person_id = {$person_id}"); $this->setName($personData->name); } public function setName($name){ $this->_name = $name; } public function getName() { return $this->_name; } public function hello(){ return "Sup yo, I'm " . $this->_name; } } ... class PersonController extends Zend_Controller_Action { public function PersonAction(){ $person_id = ...->getParam('pid'); $speaker = new Application_Model_Person($person_id); $this->view->greeting = $speaker->hello(); } } ?>
|
# ? Apr 16, 2011 06:48 |
|
I don't like it. That means you have to mock those objects each time you're testing your code because at a minimum you have to construct the SUT (system under test, or the object you're testing).
|
# ? Apr 16, 2011 11:43 |
|
Never mind
PAH-PAH POKER FACE fucked around with this message at 19:34 on Apr 16, 2011 |
# ? Apr 16, 2011 19:04 |
|
If I've created a callback, is there any way to get the content of that function as a string? Like, php:<? $a = function() { return 42; }; print($a); // "function() { return 42; }" ?>
|
# ? Apr 16, 2011 20:42 |
|
I think once the file has been parsed, it will have been turned into Zend engine bytecode, so I doubt it.
|
# ? Apr 16, 2011 22:18 |
|
qntm posted:If I've created a callback, is there any way to get the content of that function as a string?
|
# ? Apr 16, 2011 22:32 |
|
I'm using Drupal and I'm trying to create a theme with several different content types. Here's the print_r() on one sample entry, for a content type called "link":php:<? <!-- stdClass Object ( [vid] => 1 [uid] => 1 [title] => "The Title" [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 1 [type] => link [language] => und [created] => 1302727201 [changed] => 1302727201 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1302727201 [revision_uid] => 1 [body] => Array ( [und] => Array ( [0] => Array ( [value] => Blah Blah Blah [summary] => [format] => filtered_html [safe_value] => <p>Blah Blah Blah</p> [safe_summary] => ) ) ) [field_url] => Array ( [und] => Array ( [0] => Array ( [value] => [url]http://somesite.com/page.php[/url] [format] => [safe_value] => [url]http://somesite.com/page.php[/url] ) ) ) ) -->?> Referencing parts of this are easy; I can always pull in the "title" field through $node->title, but how do I print the values in field_url and body?
|
# ? Apr 16, 2011 22:56 |
|
php:<? if (property_exists('body', $node) && is_array($node->body)) { $body = $node->body['und'][0]; }?>
|
# ? Apr 16, 2011 23:34 |
|
Yay posted:
You're going to run into a cross-site scripting vulnerability if you do this exactly like that. At least run the value of body through check_markup(). However that would ignore any other module's hard work in case you have tacked on functionality that extends core Drupal 7 fields. In Drupal 6, we had to do it like that. The $content variable built as a part of template_preprocess_node is now an Array in Drupal 7 ready to be plugged into render(). You can extract the fields from $content instead of the $node object. It will look kind of like the example in field_attach_view. php:<? print render($content['body']); print render($content['field_url']); ?> They did all this just for theme developers
|
# ? Apr 18, 2011 02:46 |
|
Thanks, Yay and Spacebard. One more question... how do I get print render($content['field_url']); to insert the field I want without putting DIV tags around it? I literally just want the field's value, but it gives me this: quote:<div class="field field-name-field-url field-type-text field-label-hidden"><div class="field-items"><div class="field-item even">http ://somesite.com/blahblahblah</div></div></div> I remain convinced that Drupal's online documentation was written with the assumption that you already knew how the entire system works. EDIT: Ah, putting the render inside a strip_tags statement did it! Thanks! SETEC Astronomy fucked around with this message at 04:53 on Apr 18, 2011 |
# ? Apr 18, 2011 03:32 |
|
Could always strip_tags it
|
# ? Apr 18, 2011 03:55 |
|
SETEC Astronomy posted:I remain convinced that Drupal's online documentation was written with the assumption that you already knew how the entire system works. A lot of open source is 'documented' like that. Its the secret shame of free software. Edit; see also 'every function has a PHPDoc block as a header' == 'fully documented' (understanding how these 900 functions are bound together into functioning software is left as an exercise for the reader) KuruMonkey fucked around with this message at 08:24 on Apr 18, 2011 |
# ? Apr 18, 2011 08:21 |
|
Building a large-ish site for a client wiht many, many of forms. Most of these require the user to be logged in, so I simply added a conditional that displayed either the form or a "hey you need to log in" message. This seemed to work fine, but the client wants to allow users to fill out these forms without being logged in then log in and have the data waiting for them. The first idea I had was to add a little Ajax-powered thing at the bottom of the form to let people log in if they haven't and add a logged-in requirement to the form's validation method. Then it occurred to me that we'd also need to build a sign-up screen with the same rules, and suddenly it's all a mess. Is there a standardized way to do this properly? I've got to do this on dozens of customized form pages, so I want to find a way to satisfy the client and not bloat the code/timeline.
|
# ? Apr 18, 2011 17:35 |
|
I know that call-time pass-by-reference was allowed in older versions of PHP. But more recent versions deprecate it. Why is that? I would genuinely like to know the technical reasons why they deprecated it.
|
# ? Apr 18, 2011 19:38 |
|
klem_johansen posted:Building a large-ish site for a client wiht many, many of forms. Most of these require the user to be logged in, so I simply added a conditional that displayed either the form or a "hey you need to log in" message. This seemed to work fine, but the client wants to allow users to fill out these forms without being logged in then log in and have the data waiting for them. This should be simple enough, this is how I would do it: [logged in] Form Content -> Submit POST content to submit page -> done/thank you page [not logged in] Form Content -> Submit POST content to submit page -> Trigger login modal -> submit login info AND form info via POST to done/thank you page [not loggen in and no account] Form content -> Submit POST content to submit page -> Trigger registration modal -> pass POST content/registration info to done/thank you page Now, if you have to activate the account...I'm not sure how you would want to gracefully handle that. I would create a PHP class that's a base submit class, then your login/registration should be their own classes. These would be called from a generic submit function that will create a class instance of each thing needed. I don't know if you have a more specific question, but I'm not sure what stumbling block you're hitting. So long as you're doing some OO PHP, mixing and matching the appropriate class to instantiate based on the required arguments you can keep a complex page relatively clean.
|
# ? Apr 18, 2011 19:55 |
|
spacebard posted:You're going to run into a cross-site scripting vulnerability if you do this exactly like that.
|
# ? Apr 18, 2011 21:21 |
|
I'm rewriting an image hosting script ( http://junko.hk/junko-php-1.7.tbz2 ) I wrote a while back following R1CH's minimal script. The main goal is for HTML 5 support and to convert everything to OOP. I don't know if it is a mistake or not but I thought I would follow the conventions in the CodeIgniter framework. Holy loving lol CodeIgniter decided to implement their own i18n/l10n system and the authentication support seems pretty weak. Gettext has always been the better choice on any language for i18n and for PHP I've been preferring Pear::Auth for authentication as it as a pretty good selection of modules. Main goals are UTF-8 for i18n support, HTML 5 directory and drag & drop uploading, HTTP upload and HTTP transloads, image resizing with GD2, ImageMagick or GMagick, simple PHP templates, web based installation, validation and configuration. Ideally I want to put a full test suite in there too but I'm not sure how well PHP is up for that.
|
# ? Apr 19, 2011 11:42 |
|
php:<? // inside some_function() inside some_function_file.php $response = file_get_contents($request); if ($response === False) { echo "Response was false (msg1)<br>"; return False; } else { // parse XML $pxml = simplexml_load_string($response); if ($pxml === False) { echo "xml parsing bad (msg2)<br>"; return False; // no xml } else { echo "xml parsing good (msg3)<br>"; return $pxml; } }?> But when I am including some_function_file.php from within some_file.php, which is being included in index.php, it returns msg1 and fails to respond from the file_get_contents. in php.ini, 'allow_url_fopen = On' is set. Any ideas, please?
|
# ? Apr 19, 2011 22:44 |
|
Safety Shaun posted:
I imagine file_get_contents() searches different directories based on where the original calling script is located. Are some_file.php and index.php located in different directories?
|
# ? Apr 19, 2011 23:31 |
|
qntm posted:I imagine file_get_contents() searches different directories based on where the original calling script is located. Are some_file.php and index.php located in different directories? Correct \index.php - include('\includes\some_file.php'); \includes\some_file.php - require('some_functions.php) - calls some_function() from^ \includes\some_functions.php edit: File get contents pulls a URL which returns XML data. Safety Shaun fucked around with this message at 00:04 on Apr 20, 2011 |
# ? Apr 19, 2011 23:38 |
|
Hey what is <<< ? I saw it in some other code and I've never seen it before, can't find reference to it in the manual.
|
# ? Apr 20, 2011 00:35 |
|
Was it right next to a string declaration? I think the only place it's used is heredoc syntax strings...
|
# ? Apr 20, 2011 00:36 |
|
rt4 posted:Was it right next to a string declaration? I think the only place it's used is heredoc syntax strings... Yeah that's what it is. I was looking at operators not strings, which is probably why I didn't find it. Seems like a pretty stupid way to set up a string variable.
|
# ? Apr 20, 2011 00:43 |
|
It is but if you're coping and pasting a huge bunch of text to put into a string it's great because you don't need to do any escaping.
|
# ? Apr 20, 2011 00:50 |
|
revmoo posted:Yeah that's what it is. I was looking at operators not strings, which is probably why I didn't find it. Seems like a pretty stupid way to set up a string variable. It has it's uses, mainly when building a large block of text (for an email, etc.) Past that, not too helpful.
|
# ? Apr 20, 2011 00:52 |
|
Right I get that. In those cases I'd just use templates/views and include the dynamic content, instead of doing it the other way around. I understand why you'd use it though, even if it is kind of silly. The context in which I saw it used was not a good use though. Imagine these kinds of code blocks, repeating over and over and over in a classes (lol) file: if (!empty($avar)) { $str .= <<<EOF <span>$desc</span> EOF; } ...to generate an entire page. There's like 30 of these blocks making GBS threads up this ONE classes file. There's also files including files, including files, including files, including files. Yes, it's four deep. There's also a matching function to include php files that match a certain pattern.
|
# ? Apr 20, 2011 01:05 |
|
It's called heredoc and was taken from Perl (kinda).
|
# ? Apr 20, 2011 01:25 |
|
Safety Shaun posted:Correct Is it pulling from an absolute URL or relative URL? I ask because you have created an includes directory, but if this function file is called from your index page and you try to do something like $url = "../whatever.xml" that will fail as the page the function is called from is the page the script sees any file/path relative to. so, if I create this file structure: index.php includes/a.php includes/b.xml includes/functions.php in my functions.php is included in a.php and it's hit with domain.tld/includes/a.php and it uses the url of b.xml that will work. If you include on the index.php page and use b.xml as the URL, that will fail, you'd have to do includes/b.xml as the URL. Or I'm not reading the issue carefully enough.
|
# ? Apr 20, 2011 01:58 |
|
I believe the include path works like this in php: 1st - folder of execution 2nd - folder of current file 3rd - 'include_path' set in php.ini 4th - system 'path' variable So: if you have a structure like this: code:
php:<? // index.php include('lib/config.php'); ?> <? // lib/config.php include ('functions.php'); include ('vars.php'); include ('other.php'); ?> code:
|
# ? Apr 20, 2011 02:19 |
|
file_get_contents() will not search the includes path unless you specify a boolean true as the second argument. Otherwise if you use a relative url it will be included relative to the called PHP file, and not necessarily the php file where the function is called from (as discussed previously). One way to overcome this issue is to use dirname(__FILE__) in your file name, that will return the absolute path to the script where it is defined, regardless of the including script. (if you are using php > 5.3.0 you can use __DIR__) i.e. php:<? $path = dirname(__FILE__); file_get_contents($path.DIRECTORY_SEPARATOR."myfile.txt"); ?>
|
# ? Apr 20, 2011 15:10 |
|
Thanks guys. The I am getting is a URL from a XML based web service. I've been echoing crap out everywhere to see where my data isn't being passed, heres what i've found so far. some_functions.php - contains some_function(a,b,c,d); - some_function(a,b,c,d) contains php:<? <snip> $request = "http://".$host.$uri."?".$canonicalized_query."&Signature=".$signature; // do request $response = file_get_contents($request); echo "Debug: <a href='$request'>Query</a> = $request (response = $response)<br>"; <snip> return $someXML?> some_include.php - require_once(some_functions.php), calls some_function(); - echos query = fine, response = fine index.php - includes some_include.php - echos query = fine, response = blank
|
# ? Apr 20, 2011 15:39 |
|
What happens if you just echo out the URL and then do a code:
|
# ? Apr 20, 2011 15:43 |
|
I'm not exactly sure what you mean but I found the cURL class, but I am not receiving any errors to indicate i'm doing it wrong or any data backphp:<? $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, '$request'); $response = curl_exec ($ch); curl_close ($ch); ?>
|
# ? Apr 20, 2011 15:54 |
|
|
# ? Jun 8, 2024 00:21 |
|
For one thing, you put $request in single quotes, so it's looking for the literal string '$request'. If you want it to expand the variable, you'll need to use double quotes, or you can just leave the quotes off completely.
|
# ? Apr 20, 2011 15:57 |