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
Winkle-Daddy
Mar 10, 2007
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!

Adbot
ADBOT LOVES YOU

Winkle-Daddy
Mar 10, 2007
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:
firstValue,secondValue,thirdValue
aaa,bbb,ccc
aab,bbc,ccd
So, when I load this into a variable uh, data, and do a print_r I get back:
code:
Array ( [0] => (Array ( [firstValue] => aaa [secondValue] => bbb [thirdValue] => ccc)
[1] => (Array ( [firstValue] => aab [secondValue] => bbc [thirdValue] => ccd) )
since this CSV file is like 28,000 lines, I am doing this horrible inefficient code:

code:
$valueCheck = 'aab';
if(in_array($valueCheck, $data)) {
    $i = 0;
    while($data[$i]['firstValue] != $valueCheck) {
        $i++;
    }
    print $data[$i]['secondValue'];
}
else {
    print "Value is not found";
}
Basically, in_array doesn't work with a multidimensional array...is there an easy way to compensate for that?

Otherwise, I'm going to do it this way instead, which I don't like quite as much:

code:
<?php
$dataOut = array();
$handle = fopen("in.csv","r");
if ($handle) {
   $dataKeys = explode(",",fgets($handle, 4096));
   while (($buffer = fgets($handle, 4096)) !== false) {
       $dataOut = array_fill_keys($dataKeys,explode(",",$buffer));
   }
   if (!feof($handle)) {
       echo "Error: unexpected fgets() fail\n";
   }
   fclose($handle);
}
Print_r($dataOut);
?>
e: in case anyone tries to use this...it's broken, FYI.

Ideas on how I could be doing this better?

Winkle-Daddy fucked around with this message at 23:17 on Apr 13, 2011

KuruMonkey
Jul 23, 2004

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);
}
?>
Thats what I would use, there's a second param for fgetcsv() that you might need to deal with. If you don't have fgetcsv() avaliable - I'd recommend writing it in order to use it - or raise hell until you can have a workable server environment.

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

Winkle-Daddy
Mar 10, 2007
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.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

Nichololas posted:

I debated briefly but figured this thread would be more appropriate than the Java questions that don't need their own thread..

Anyone have experience using PHP/Java Bridge? I'm using PHP to display a small website and I need to call two functions from a Java class.

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.

Lt Moose
Aug 8, 2007
moose

musclecoder posted:

Quick and dirty:

php:
<?
$file = file_get_contents('http://path/to/that/document');
$file_raw = stripslashes($file);

// Do stuff with $file_raw now?>
I can't believe I just recommended using stripslashes().
Thank you! This worked perfectly.

Baggy_Brad
Jun 9, 2003

THUNDERDOME LOSER
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();
    }
}
?>
Or, doing the DB instation and look up in the object constructor

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();
    }
}
?>

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.
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).

PAH-PAH POKER FACE
Nov 7, 2009
Never mind

PAH-PAH POKER FACE fucked around with this message at 19:34 on Apr 16, 2011

qntm
Jun 17, 2009
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; }"
?>

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
I think once the file has been parsed, it will have been turned into Zend engine bytecode, so I doubt it.

Yay
Aug 4, 2007

qntm posted:

If I've created a callback, is there any way to get the content of that function as a string?
I've never dealt with anonymous callbacks like that, nor reflection outside of classes, but the export method of ReflectionFunction may be able to help.

SETEC Astronomy
Jul 30, 2008

Too many secrets.
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]
                        )
 
                )
 
        )
)
 -->?>
I've skipped all the rest of the nonsense because it doesn't seem relevant to the question I'm about to ask. Oh, and those stupid [ url ] tags were put there by the forum, not me.

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?

Yay
Aug 4, 2007
php:
<?
if (property_exists('body', $node) && is_array($node->body)) {
    $body = $node->body['und'][0];
}?>
should do it. Node is a simple object (StdClass), but body, field_url and their children are standard arrays.

spacebard
Jan 1, 2007

Football~

Yay posted:

php:
<?
if (property_exists('body', $node) && is_array($node->body)) {
    $body = $node->body['und'][0];
}?>
should do it. Node is a simple object (StdClass), but body, field_url and their children are standard arrays.

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']);
?>
And then $content['body'] ALSO has a copy of the node object in it :psyduck:

They did all this just for theme developers :v:

SETEC Astronomy
Jul 30, 2008

Too many secrets.
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

Impotence
Nov 8, 2010
Lipstick Apathy
Could always strip_tags it

KuruMonkey
Jul 23, 2004

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

klem_johansen
Jul 11, 2002

[be my e-friend]
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.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
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.

Winkle-Daddy
Mar 10, 2007

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.

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.

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.

Yay
Aug 4, 2007

spacebard posted:

You're going to run into a cross-site scripting vulnerability if you do this exactly like that.
To clarify, I assume you're making a point specific to Drupal here, as the snippet I provided suggests nothing of the sort? Also, does drupal really not auto-escape in templates by default?

MrMoo
Sep 14, 2000

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 :suicide: 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.

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
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;
        }
    }?>
When I am including some_function_file.php from within some_file.php, the above returns okay (msg3).

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?

qntm
Jun 17, 2009

Safety Shaun posted:

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;
        }
    }?>
When I am including some_function_file.php from within some_file.php, the above returns okay (msg3).

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?

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?

Safety Shaun
Oct 20, 2004
the INTERNET!!!1

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

revmoo
May 25, 2006

#basta
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.

spiritual bypass
Feb 19, 2008

Grimey Drawer
Was it right next to a string declaration? I think the only place it's used is heredoc syntax strings...

revmoo
May 25, 2006

#basta

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.

spiritual bypass
Feb 19, 2008

Grimey Drawer
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.

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

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.

revmoo
May 25, 2006

#basta
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.

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.
It's called heredoc and was taken from Perl (kinda).

Winkle-Daddy
Mar 10, 2007

Safety Shaun posted:

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.

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.

bobthecheese
Jun 7, 2006
Although I've never met Martha Stewart, I'll probably never birth her child.
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:
index.php
vars.php
other.php
lib/functions.php
lib/config.php
lib/other.php
Then the files are like this:
php:
<?
// index.php
include('lib/config.php');
?>

<?
// lib/config.php
include ('functions.php');
include ('vars.php');
include ('other.php');
?>
Then you will get these files included in this order
code:
index.php
lib/config.php
lib/functions.php
vars.php
other.php

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.
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");
?>

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
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?>
returns XML;

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

musclecoder
Oct 23, 2006

I'm all about meeting girls. I'm all about meeting guys.
What happens if you just echo out the URL and then do a
code:
curl http://path/to/xml.xml
to see what happens from the command line?

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
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 back
php:
<?
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, '$request');
        $response = curl_exec ($ch);
        curl_close ($ch);
?>
As per above debug echos, query = fine (accessible and viewable by URL, returns XML to browser), response = blank

Adbot
ADBOT LOVES YOU

butt dickus
Jul 7, 2007

top ten juiced up coaches
and the top ten juiced up players
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.

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