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
Hammerite
Mar 9, 2007

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

Golbez posted:

Another crazy-rear end question...

Let's say a function returns an array. Is it possible to immediately ask for a value from this array?

Unfortunately no. That will give a syntax error. You need to assign the array to a variable and then refer to an offset of that variable, as you did in your second example.

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

Hammerite posted:

Unfortunately no. That will give a syntax error. You need to assign the array to a variable and then refer to an offset of that variable, as you did in your second example.

Thanks. I had a feeling there was no way but I wanted to check.

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
Another quirk I just ran across:
code:
$x = &$_SESSION['foo'];
works, whereas...
code:
$x = (isset($_SESSION['foo']))?&$_SESSION['foo']:false;
does not. Any ideas why not? Can you not mix the ternary operator with a pass by reference? Doesn't work if I slide a space between the ? and &, either. PHP 5.2.8.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Hammerite posted:

Unfortunately no. That will give a syntax error. You need to assign the array to a variable and then refer to an offset of that variable, as you did in your second example.

Well, if he doesn't want to keep the array, he can use a different function to extract the value:

code:
$stringICareAbout = current(explode(' ', 'IMPORTANT_THING and some other bullshit - hay want to see some baby pictures?'));

$otherImportantThing = current(array_slice(explode(' ', 'The treasure is in the HERE but you are over THERE'), 5, 1));
But, yeah, PHP Sucks Exhibit n+1

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

Munkeymon posted:

Well, if he doesn't want to keep the array, he can use a different function to extract the value:

code:
$stringICareAbout = current(explode(' ', 'IMPORTANT_THING and some other bullshit - hay want to see some baby pictures?'));

$otherImportantThing = current(array_slice(explode(' ', 'The treasure is in the HERE but you are over THERE'), 5, 1));
But, yeah, PHP Sucks Exhibit n+1

Yeah, I had no interest in the rest of the array (it was the return from getimagesize, I was interested only in the mimetype, though since then I've wanted to also get the actual dimensions, so it's no longer a relevant issue), so I was hoping there was a simple way to call the function and immediately get an array value out of it, then discard the array, in one line.

I suspect, if such a thing were possible, there'd be a lot of people who would have already learned to combine mysql_fetch_assoc with a return. I know I would enjoy being able to do "return mysql_fetch_assoc($result)['actual data i'm looking for']".

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

Golbez posted:

I suspect, if such a thing were possible, there'd be a lot of people who would have already learned to combine mysql_fetch_assoc with a return. I know I would enjoy being able to do "return mysql_fetch_assoc($result)['actual data i'm looking for']".

Fat chance, because that would be too expressive and thought out.

Sub Par
Jul 18, 2001


Dinosaur Gum
Does anyone have any experience with this class for sending SMS via php using Google voice? Any better ideas for sending them? I haven't found any free gateways, and it seems trivially easy to sign myself up for Google Voice. Thoughts?

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!

Sub Par posted:

Does anyone have any experience with this class for sending SMS via php using Google voice? Any better ideas for sending them? I haven't found any free gateways, and it seems trivially easy to sign myself up for Google Voice. Thoughts?

Is there a problem with using this to send through google voice? It works well, I just tested it and it is pretty straight forward.

Sub Par
Jul 18, 2001


Dinosaur Gum

DarkLotus posted:

Is there a problem with using this to send through google voice? It works well, I just tested it and it is pretty straight forward.

I haven't tested it yet - don't have a google voice account yet. Wanted to know if it all worked and wasn't a broken mess before I got started. I'm new to PHP and just wanting to make sure I don't inadvertently use a bunch of poo poo I regret later. Thanks.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

Golbez posted:

Another crazy-rear end question...

Let's say a function returns an array. Is it possible to immediately ask for a value from this array?

This will be possible in the next major version of PHP.

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!

Sub Par posted:

I haven't tested it yet - don't have a google voice account yet. Wanted to know if it all worked and wasn't a broken mess before I got started. I'm new to PHP and just wanting to make sure I don't inadvertently use a bunch of poo poo I regret later. Thanks.

Give it a shot, I looks like it will do what you want.

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

McGlockenshire posted:

This will be possible in the next major version of PHP.

:monocle: Now we just have to wait for the release, which has been in the works since... 2005? Depending on if it's 5.4 or 6.0...

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

Golbez posted:

I suspect, if such a thing were possible, there'd be a lot of people who would have already learned to combine mysql_fetch_assoc with a return. I know I would enjoy being able to do "return mysql_fetch_assoc($result)['actual data i'm looking for']".

You could do this with PDO, something like

code:
$myValue = $myPdoStatment->Fetch(PDO::FETCH_OBJ)->MyColumn;
But you are really splitting hairs here. Yeah its nice to use that kind of stuff, but really whats a few extra lines of code?

Also, try and stay away from references. Its just a quagmire in PHP. There is really no reason to use them, and references are slower in php anyways. You are better off using a little more code to read a session variable, then set it with a new value later.

Yay
Aug 4, 2007

Begby posted:

You could do this with PDO, something like [...]
Functionality, yes, you can do the equivalent thanks to method chaining. In practice, you're doing two different things, one of which simply cannot be done in PHP, regardless of whether its PDO or not (object property access vs. array key access)

Begby posted:

But you are really splitting hairs here. Yeah its nice to use that kind of stuff, but really whats a few extra lines of code?
Honestly? As someone who used to work exclusively in PHP, when I need to come back to it from Python (which has had the feature for as long as I can remember),this is the kind of small thing I really miss (excusing the obvious warts of both languages).

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

Yay posted:

Honestly? As someone who used to work exclusively in PHP, when I need to come back to it from Python (which has had the feature for as long as I can remember),this is the kind of small thing I really miss (excusing the obvious warts of both languages).

Yeah, I guess you are right. Now that I have migrated to mostly C# every time I go back to PHP I kinda want to shoot myself over stuff like this. Hopefully they improve the language, PHP certainly has its utility, hopefully it gets better.

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
This is probably a mental block on being unable to find the proper function, but: I have an image stored in the database. I get that image out. Now, to display it, I simply run a few headers and then echo the variable. But what if I want to manipulate it first, create a resampled thumbnail for example? All of the imagecreatefrom* functions require filenames, I don't see any function for simply injecting data directly in. Will I have to write a file, then process that, or is there a way to create an image directly from a variable?

Yay
Aug 4, 2007

Golbez posted:

All of the imagecreatefrom* functions require filenames, I don't see any function for simply injecting data directly in.
imagecreatefromstring doesn't.

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

Yay posted:

imagecreatefromstring doesn't.

Well there we go, as I said, it was probably just a case of me missing the right command in the list. Thanks. :)

Edit: Aha, but is it possible to get the image dimensions without writing it to a file and running getimagesize()?

Golbez fucked around with this message at 21:50 on Aug 6, 2010

Yay
Aug 4, 2007

Golbez posted:

Aha, but is it possible to get the image dimensions without writing it to a file and running getimagesize()?
imagesX and imagesY.

No, I don't know why the functions are named as they are.

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

Yay posted:

imagesX and imagesY.

No, I don't know why the functions are named as they are.

Thanks again :)

uh zip zoom
May 28, 2003

Sensitive Thugs Need Hugs

I've been away from PHP for quite some time, and I'm trying to get back into it by creating my own website. Anyway, index.php has been coded to call the following function:

code:
function displayTopMenu ($siteSection, $currentPageFileName) {
	
	if ($siteSection == "normal"):
		//  again, hardcoded until I get the database up
		$navPages = array ( 
			"pageNames" => array (1 => "Home", "Blog", "Photos", "Links", "Contact"),
			"pagePaths" => array (1 => "index.php", "blog.php", "photos.php", "links.php", "contact.php")
		);

		echo "<ul>";
			for ($i = 1; $i <= 5; $i++):	
				echo "<li";
				// gives list item current_page_item class if it is, in fact, the current page
				if ($currentPageFileName == $navPages["pagePaths"][i]):
					echo " class=\"current_page_item\"";
				endif;
				echo "><a href=\"";
				print ($navPages["pagePaths"][i]);
				echo "\">";
				print ($navPages["pageNames"][i]);
				echo "</a></li>";
			endfor;
		echo "</ul>";
	endif;
}
It's very simple function to display my horizontal nav menu. To be thorough, here's the php executed by index.php. The first bit is some procedural code to set my variables, the second part is the function call:

code:
<?php 
//procedural code executed before page loads
include("../code/functions.php"); 

// take file path and break it up by "/,"  putting the elements into an array.  
$currentFilePath = $_SERVER["SCRIPT_NAME"];
$pagePathArray = explode("/", $currentFilePath);
//the last element in the array
$pagePathArrayMaxElements = end($pagePathArray);
echo $pagePathArrayMaxElements;
?>
And finally, the function call:

code:
<?php displayTopMenu ("normal",  $pagePathArrayMaxElements); ?>
The problem I'm getting is that the values in the two-dimensional array I created to store my page titles and filenames are not repeat not being printed. I have no idea why. I've even run print_r on the array and all of the data is there. I can't look at this anymore, so I'm going to bed. Hopefully some kind goon can save me tonight.

And just to be thorough, here's what the function returns:

code:
<ul><li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li><li><a href=""></a></li></ul>

uh zip zoom fucked around with this message at 03:39 on Aug 7, 2010

Yay
Aug 4, 2007
code:
print ($navPages["pagePaths"][i]);
You're missing a dollar token, i is not $i.

gwar3k1
Jan 10, 2005

Someday soon
edit: ^^^ beaten and more concisely

Maybe it works but I wouldn't think its good practice to mix an array as being part named, part unnamed.

You have:

php:
<?
$$navPages = array ("pageNames" => array (1 => "Home", "Blog", "Photos", "Links", "Contact")...);
?>
Either remove the "1 =>" of give all of items names - probably the former unless you change the code when you pull it from the database.

Secondly, your index variable isn't properly used. You have ignored the $:

php:
<?
$arrname["arritem"][i]  // currently
$arrname["arritem"][$i] // should have a $
?>
Finally, rather than echo/printing everything, build and return a string.

php:
<?
Function 
{
  $nav  = "<ul>\n"; // \n will force a new line in the output code
  for ($i = 1; $i <= 5; $i++)
  { 
    $nav .= "  <li"; // The .= appends rather than replaces a string
    if ($currentPageFileName == $navPages["pagePaths"][$i])
    {
      $nav .= " class=\"current_page_item\"";
    }
    $nav .= "><a href=\""".$navPages["pagePaths"][$i]."\">".$navPages["pageNames"][$i]."</a>\n  </li>\n";
  }
  $nav .= "</ul>";

  return $nav;
}

$printnav = function(a,b);
echo $printnav;
?>
More than likely just changing the second problem will fix your code.

uh zip zoom
May 28, 2003

Sensitive Thugs Need Hugs

gwar3k1 posted:

edit: ^^^ beaten and more concisely

Maybe it works but I wouldn't think its good practice to mix an array as being part named, part unnamed.

You have:
coda bottom pull
php:
<?
$$navPages = array ("pageNames" => array (1 => "Home", "Blog", "Photos", "Links", "Contact")...);
?>
Either remove the "1 =>" of give all of items names - probably the former unless you change the code when you pull it from the database.

Secondly, your index variable isn't properly used. You have ignored the $:

php:
<?
$arrname["arritem"][i]  // currently
$arrname["arritem"][$i] // should have a $
?>
Finally, rather than echo/printing everything, build and return a string.

php:
<?
Function 
{
  $nav  = "<ul>\n"; // \n will force a new line in the output code
  for ($i = 1; $i <= 5; $i++)
  { 
    $nav .= "  <li"; // The .= appends rather than replaces a string
    if ($currentPageFileName == $navPages["pagePaths"][$i])
    {
      $nav .= " class=\"current_page_item\"";
    }
    $nav .= "><a href=\""".$navPages["pagePaths"][$i]."\">".$navPages["pageNames"][$i]."</a>\n  </li>\n";
  }
  $nav .= "</ul>";

  return $nav;
}

$printnav = function(a,b);
echo $printnav;
?>
More than likely just changing the second problem will fix your code.

thank you so much you guys. I've been programming c# for the past three years, and coming back to php makes me feel like an old man just let out of a nursing home for a day pass... :corsair:

Edit: the only reason I named the first element in each nested array was because I wanted the index to start at one instead of zero, because it's easier for my peon brain to understand.

uh zip zoom fucked around with this message at 19:34 on Aug 7, 2010

gibbed
Apr 10, 2006

Yay posted:

imagesX and imagesY.

No, I don't know why the functions are named as they are.
I've always figured it's imageSX and imageSY (as in, image size x, image size y).

DholmbladRU
May 4, 2006
I am attempting to create an update profile page on thie website, however I am having some trouble keeping multiple forms on the same page from affecting each other.

here is some psudo code for what I want to do
code:


execute if form1 was "submitted" {
some code		
}

execute if form2 was "submitted" {
    some code
	}
By searching online someone sugessted you name the forms, and then use the $_REQUEST to see if they are set.

php:
<?
    
if (isset($_REQUEST['submit2'])) { 
    if($Password1 = $Password2) 
        {
            code
        
            }
            
        }
?>

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

DholmbladRU posted:

I am attempting to create an update profile page on thie website, however I am having some trouble keeping multiple forms on the same page from affecting each other.

here is some psudo code for what I want to do
code:


execute if form1 was "submitted" {
some code		
}

execute if form2 was "submitted" {
    some code
	}
By searching online someone sugessted you name the forms, and then use the $_REQUEST to see if they are set.

php:
<?
    
if (isset($_REQUEST['submit2'])) { 
    if($Password1 = $Password2) 
        {
            code
        
            }
            
        }
?>

Just use a hidden form field and give it a value. Then check the value in your form handler.

As with all form data, it can easily be changed client side, so keep that in mind as far as security is concerned.

DholmbladRU
May 4, 2006

Begby posted:

Just use a hidden form field and give it a value. Then check the value in your form handler.

As with all form data, it can easily be changed client side, so keep that in mind as far as security is concerned.

php:
<?
<input type="hidden" name="password_Form" value="1"/>?>
Would each form have its own name. Or would they all be nammed the same with different values, and you check value?



As far as security is concerened, should I pass the variable through php functions to ensure it doesnt contain sql injection or any slashs?

DholmbladRU fucked around with this message at 18:26 on Aug 9, 2010

gwar3k1
Jan 10, 2005

Someday soon

DholmbladRU posted:

php:
<?
<input type="hidden" name="password_Form" value="1"/>?>
Would each form have its own name. Or would they all be nammed the same with different values, and you check value?

As far as security is concerened, should I pass the variable through php functions to ensure it doesnt contain sql injection or any slashs?

Each form has its own name, then check it.

php:
<?
if(isset($_POST['password_Form']) && $_POST['password_Form'] = <value>)
{
  doStuff;
}
?>
You could create a random value and store it in the session array then check that the two match. That would solve the problem of people modifying the value in the url at least.

The security comment, I think, was to highlight the fact that a user doesn't have to submit the form to trigger the actions. They can trick the script into thinking a form has been submitted with the data they want sent.

If anything is going into SQL statements, it needs to be sanitized first.

A hamburger?
Jul 16, 2004
I feel like an idiot, but I have been forced to parse an XML file through PHP, and never having had to work with XML before am failing horribly.

After looking through dozens of xml namespace things, I still have no idea how to use them. All I know is that I can parse through regular nodes with $xml->children() but that doesn't work for getting the values of nodes with namespaces, or so it seems.

I'm working with an xml file that I can't change. The top of the xml file has the xmlns defined... Do I need to do something else like register the namespace in my processing document? Do I have to make calls to xpath just to get the values? I am completely lost and the tutorials/documentation for this gets me nowhere... help?

DholmbladRU
May 4, 2006
Okay, I got the seperate forms all worked out. Now what I am doing is echo infromation from the database into a table on the same page which a user can update his profile.

Is there a way to make it so the page does not have to be manually refreshed, and will update the table.


"to display the webpage again internet explorer needs to resend the information you've previouslly submitted"

This message pops up when I manually refresh the page.

DholmbladRU fucked around with this message at 19:10 on Aug 9, 2010

gwar3k1
Jan 10, 2005

Someday soon

DholmbladRU posted:

Okay, I got the seperate forms all worked out. Now what I am doing is echo infromation from the database into a table on the same page which a user can update his profile.

Is there a way to make it so the page does not have to be manually refreshed, and will update the table.


"to display the webpage again internet explorer needs to resend the information you've previouslly submitted"

This message pops up when I manually refresh the page.

The method is called Post / Redirect / Get. I don't fully understand the PHP code for it, so I'll let you research it.

Masked Pumpkin
May 10, 2008

DholmbladRU posted:

Okay, I got the seperate forms all worked out. Now what I am doing is echo infromation from the database into a table on the same page which a user can update his profile.

Is there a way to make it so the page does not have to be manually refreshed, and will update the table.


"to display the webpage again internet explorer needs to resend the information you've previouslly submitted"

This message pops up when I manually refresh the page.

Your best bet would be to look into ajax if you don't want to refresh the page, though that can be quite daunting at first glance. For such a simple task, this might help.

uh zip zoom
May 28, 2003

Sensitive Thugs Need Hugs

How are PHP developers these days managing data? I've got MySQL 5.1.something or other, and I would ideally like to use stored procedures. Reading the MySQL manual shows support for procedures, but when I try to run a raw SQL script to create one in phpMyAdmin, I'm getting errors. Would this belong in the SQL thread? Are PHP and MySQL as intertwined as they were five years ago?

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

uh zip zoom posted:

How are PHP developers these days managing data? I've got MySQL 5.1.something or other, and I would ideally like to use stored procedures. Reading the MySQL manual shows support for procedures, but when I try to run a raw SQL script to create one in phpMyAdmin, I'm getting errors. Would this belong in the SQL thread? Are PHP and MySQL as intertwined as they were five years ago?

PHP and MySql aren't really intertwined at all, never really have been. Each runs just fine by itself. MySql is accessible by just about any language out there.

We have been using Stored Procedures with MySql for a long time where I work. We have never touched phpMyAdmin though, only have ever used gui clients. MySql has a free one, my favorite though by far is EMS MySql Manager, but it does cost a bit.

You can also write a script into a .sql file and run it on the command line if you are feeling adventurous.

McGlockenshire
Dec 16, 2005

GOLLOCKS!

A hamburger? posted:

After looking through dozens of xml namespace things, I still have no idea how to use them. All I know is that I can parse through regular nodes with $xml->children() but that doesn't work for getting the values of nodes with namespaces, or so it seems.
Sounds like you're using the DOM. The functions that end in "NS" all have to do with namespaces. They'll be helpful.

Unfortunately that's the limit of my knowledge on the matter...

MrMoo
Sep 14, 2000

uh zip zoom posted:

How are PHP developers these days managing data? I've got MySQL 5.1.something or other, and I would ideally like to use stored procedures. Reading the MySQL manual shows support for procedures, but when I try to run a raw SQL script to create one in phpMyAdmin, I'm getting errors.

You cannot call stored procedures in phpmyadmin, create a command line PHP script to perform unit tests. Or that's how I overcame the issue.

The other mess is calling procedures in PDO, try to stick to parameters IN and objects or single returns out.
php:
<?
$sth = $dbh->prepare ('CALL sp_category_get( :collections_name, :category_name )');
$sth->bindParam (':collections_name', $collections_name, PDO::PARAM_STR);
$sth->bindParam (':category_name', $category_name, PDO::PARAM_STR);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_CLASS, 'PDO_Categories');
$category = $sth->fetch();
$sth->closeCursor();?>

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

A hamburger? posted:

I feel like an idiot, but I have been forced to parse an XML file through PHP, and never having had to work with XML before am failing horribly.

After looking through dozens of xml namespace things, I still have no idea how to use them. All I know is that I can parse through regular nodes with $xml->children() but that doesn't work for getting the values of nodes with namespaces, or so it seems.

I'm working with an xml file that I can't change. The top of the xml file has the xmlns defined... Do I need to do something else like register the namespace in my processing document? Do I have to make calls to xpath just to get the values? I am completely lost and the tutorials/documentation for this gets me nowhere... help?

Two pages ( maybe 3? ) ago in this thread there was some discussion on PHP, XML, and namespaces. I can't remember what the exact question / resolution was, but might be a good read to see if it helps in your case.

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
Another GD question: So I've created an image resource with imagecreatetruecolor, and I've manipulated it with imagecopyresampled... now how do I get the image data into the database? Right now, I write the image back to the filesystem with imagejpeg, and then fopen/fread the data into a variable. Surely there's a way to skip that step? I don't think saving the resource will work, so how do I get the data out of the resource?

Adbot
ADBOT LOVES YOU

Alex007
Jul 8, 2004

Golbez posted:

Another GD question: So I've created an image resource with imagecreatetruecolor, and I've manipulated it with imagecopyresampled... now how do I get the image data into the database? Right now, I write the image back to the filesystem with imagejpeg, and then fopen/fread the data into a variable. Surely there's a way to skip that step? I don't think saving the resource will work, so how do I get the data out of the resource?

Assuming $tmp is your image resource:

php:
<?
    ob_start();
    imagejpeg($tmp, NULL, 100);  break; // best quality

    $image_text = ob_get_contents();
    ob_end_clean();
?>
But don't save images into a database. Databases are for data, the filesystem is for files.

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