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
functional
Feb 12, 2008

DaTroof posted:

It's really not all that non-trivial. Here's a solution I hacked together in a few minutes.

php:
<?
class IntegerSet {
    var $value;
    var $count;
}
?>

Interesting approach to the problem. I kept trying to find a functional solution. I had thought about using a special comparator but I couldn't figure out how to tell it the external $count data. I never thought about using objects.

Adbot
ADBOT LOVES YOU

iamstinky
Feb 4, 2004

This is not as easy as it looks.
php:
<?
$_SESSION['current_vendor'] or $_SESSION['current_vendor']    = $r[2];
?>
Can someone explain to me what the person who wrote this might have thought they were doing? I mean it makes me feel like a billion of my brain cells just committed suicide every time I try to figure what he was doing here. For what it's worth $r[2] is part of a result set that should contain the vendor information.

:saddowns:

bt_escm
Jan 10, 2001

iamstinky posted:

php:
<?
$_SESSION['current_vendor'] or $_SESSION['current_vendor']    = $r[2];
?>
Can someone explain to me what the person who wrote this might have thought they were doing? I mean it makes me feel like a billion of my brain cells just committed suicide every time I try to figure what he was doing here. For what it's worth $r[2] is part of a result set that should contain the vendor information.

:saddowns:

if $_SESSION['cuerrent_vendor'] doesn't have a value or is equal to '' or 0, set $_SESSION['current_vendor'] to the value of $r[2]?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


iamstinky posted:

php:
<?
$_SESSION['current_vendor'] or $_SESSION['current_vendor']    = $r[2];
?>
Can someone explain to me what the person who wrote this might have thought they were doing? I mean it makes me feel like a billion of my brain cells just committed suicide every time I try to figure what he was doing here. For what it's worth $r[2] is part of a result set that should contain the vendor information.

:saddowns:

It's just a stupid way to do
php:
<?
if (empty($_SESSION['current_vendor']))
    $_SESSION['current_vendor'] = $r[2];
?>

iamstinky
Feb 4, 2004

This is not as easy as it looks.
I hate the guy that worked here before me so much.

EDIT:
Of the six or so apps that I enhance, unfuck and add functionality to, I have at least 10 pages that are 1000+ line switch statements that just end because the one loving function the guy bothered to write in his entire life calls something that includes something that started an output buffer and defined a preg_replace that calls a template and then somehow magically shits out a page that is functional.

iamstinky fucked around with this message at 21:39 on Aug 12, 2008

Stephen
Feb 6, 2004

Stoned

iamstinky posted:

I hate the guy that worked here before me so much.

I'm with you on that one.. I'm currently working on a PHP index page that has a grand total of NINE mysql_connect()s for two seperate databases.

nbv4
Aug 21, 2002

by Duchess Gummybuns
How do you add a indexed value to an array via indirection?

I have this:

code:
foreach(array(cat1, cat2, cat3), as $category)
{
   [...]

    $$category['xc_dual'] = $temp[0];

}

var_dump($cat1);

The cat1, cat2, etc. arrays are already created and have values. I need to add temp[0] to the end (or beginning, or wherever) and it must be INDEXED. With the above code all that comes out is the original array with no 'xc_dual'.

Whats odd is that I can change that line to:

code:
$$category[] = $temp[0];
and it works perfectly fine, but then it's indexed to "0". This is hosed up as hell.

MrEnigma
Aug 30, 2004

Moo!

nbv4 posted:

How do you add a indexed value to an array via indirection?

I have this:

code:
foreach(array(cat1, cat2, cat3), as $category)
{
   [...]

    $$category['xc_dual'] = $temp[0];

}

var_dump($cat1);

The cat1, cat2, etc. arrays are already created and have values. I need to add temp[0] to the end (or beginning, or wherever) and it must be INDEXED. With the above code all that comes out is the original array with no 'xc_dual'.

Whats odd is that I can change that line to:

code:
$$category[] = $temp[0];
and it works perfectly fine, but then it's indexed to "0". This is hosed up as hell.

It's because of how it evaluates.

Variable variables work by doing...

Getting value (string) of $category['xc_dual']
now you have $string

I think what you are assuming this is doing is...
evaluating category to a string, then setting $string['xc_dual'] and it doesn't work that way.

Edit: I just reread what I wrote and it's consuing, but there are some examples of what you want to do on the Variable Variables page.

http://us2.php.net/language.variables.variable

MrEnigma fucked around with this message at 07:53 on Aug 13, 2008

nbv4
Aug 21, 2002

by Duchess Gummybuns
Oh I see, order of operations. I'm surprised it didnt bring up an error or something... Anyways, I guess a double array is my only option:

$times[$category]['xc_dual'] = $temp[0];

MrEnigma
Aug 30, 2004

Moo!

nbv4 posted:

Oh I see, order of operations. I'm surprised it didnt bring up an error or something... Anyways, I guess a double array is my only option:

$times[$category]['xc_dual'] = $temp[0];

If you increased error_reporting to E_ALL | E_STRICT, it would throw a nice notice for you.

weekoldsushi
Jul 21, 2005

there is balm is gilead
Hey guys,

Say I wanted to get a file's base name and extension when I upload it somewhere. How would I go about doing that?

For example, if I have this in the html:
code:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000000" />
<b>Choose a document to upload: </b><br> <input name="filename" type="file" />
<p>
<input type="submit" name="submitdoc" value="SUBMIT" />
</p>
</form>
What would I have to do in uploader.php for it to assign the extension to $ext and the base name to $basename?
I've tried these and they just echo null:
code:
$file_filename = $_POST['filename'];
$basename = substr($file_filename, 0, strripos($file_filename, '.')); // strip extention
$ext = substr($file_filename, strrpos($file_filename, '.') + 1);
It's kind of frustrating since I don't know how to word it in Google.

Stephen
Feb 6, 2004

Stoned

weekoldsushi posted:

...
http://ca3.php.net/manual/en/features.file-upload.php

This should get you going on file posting in PHP.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Stephen posted:

http://ca3.php.net/manual/en/features.file-upload.php

This should get you going on file posting in PHP.

As well as pathinfo().

weekoldsushi
Jul 21, 2005

there is balm is gilead

Stephen posted:

http://ca3.php.net/manual/en/features.file-upload.php

This should get you going on file posting in PHP.

duz posted:

As well as pathinfo().

You two are PLAYERS. Thanks!

weekoldsushi
Jul 21, 2005

there is balm is gilead
Actually I kinda blew me load a little earlier than expected.
Still having problems with pathinfo().

If I do this:
code:
$file_filename = $_FILES['filename']['name'];
$file_fileinfo = pathinfo($file_filename);

echo $file_info['extension'], "<p>";
echo $file_info['basename'], "<p>";
echo $file_info['filename'], "<p>";
It won't echo anything for some reason. Echoing $file_filename returns aux1.gif, but the array in pathinfo is completely empty. Unless I set $file_filename to the actual string 'aux1.gif' it won't fill the pathinfo array with anything. What's the deal with that?

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

weekoldsushi posted:

Actually I kinda blew me load a little earlier than expected.
Still having problems with pathinfo().

If I do this:
code:
$file_filename = $_FILES['filename']['name'];
[b]$file_fileinfo = pathinfo($file_filename); // <-- Check your variable names[/b]

echo $file_info['extension'], "<p>";
echo $file_info['basename'], "<p>";
echo $file_info['filename'], "<p>";
It won't echo anything for some reason. Echoing $file_filename returns aux1.gif, but the array in pathinfo is completely empty. Unless I set $file_filename to the actual string 'aux1.gif' it won't fill the pathinfo array with anything. What's the deal with that?

$file_fileinfo should be $file_info?

If you're looking for the full path for the uploaded file on the remote machine, e.g. c:\My Documents\image.gif, that information doesn't get submitted. If you want the local path of the uploaded file on your server, check $_FILES['filename']['tmp_name'] instead.

weekoldsushi
Jul 21, 2005

there is balm is gilead

DaTroof posted:

$file_fileinfo should be $file_info?

If you're looking for the full path for the uploaded file on the remote machine, e.g. c:\My Documents\image.gif, that information doesn't get submitted. If you want the local path of the uploaded file on your server, check $_FILES['filename']['tmp_name'] instead.

I'm an idiot this always happens. Thank you very much. I'm sorry for wasting your time.

waffle iron
Jan 16, 2004

weekoldsushi posted:

code:
$file_filename = $_POST['filename'];
$basename = substr($file_filename, 0, strripos($file_filename, '.')); // strip extention
$ext = substr($file_filename, strrpos($file_filename, '.') + 1);
It's kind of frustrating since I don't know how to word it in Google.
Why don't you use basename proper?

http://php.net/basename

Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.
An easy question (probably):


I have a record in a database that contains the < and > characters (Entrails<VP> is the element in question). When I generate an html page based on that data, the interpreter strips them off as an html tag. So:

Entrails
Entrails<VP>

becomes

Entrails
Entrails

causing some confusion.


Without manipulating the data in the database, how can I force PHP to produce a page that displays the <> characters?

functional
Feb 12, 2008

Agrikk posted:

Without manipulating the data in the database, how can I force PHP to produce a page that displays the <> characters?

htmlspecialchars

Agrikk
Oct 17, 2003

Take care with that! We have not fully ascertained its function, and the ticking is accelerating.
Thanks.

Zorilla
Mar 23, 2005

GOING APE SPIT

weekoldsushi posted:

code:
echo $file_info['extension'], "<p>";
echo $file_info['basename'], "<p>";
echo $file_info['filename'], "<p>";

<p> tags are block-level elements, by the way, not an equivalent of a double <br />. They go around paragraphs (or otherwise grouped text). Also, add \n after each line or it displays as one long line in HTML source.

Zorilla fucked around with this message at 22:48 on Aug 14, 2008

vagina rodeo
Jul 2, 2005

vulva puppets, that's what
How do you call a function in a way that it uses some default arguments, but takes a value for others?

the function definition:
function query ($q,$sort_order = "NULL",$where_clause = "", $show_query = false, $page = 0)

the call I'm trying to make:
//I don't want to show the query, but I do want to return a non-default results page
$participants->query($q,$sort_order,$where_clause,,$the_page);

Alex007
Jul 8, 2004

vagina rodeo posted:

How do you call a function in a way that it uses some default arguments, but takes a value for others?

the function definition:
function query ($q,$sort_order = "NULL",$where_clause = "", $show_query = false, $page = 0)

the call I'm trying to make:
//I don't want to show the query, but I do want to return a non-default results page
$participants->query($q,$sort_order,$where_clause,,$the_page);

I don't think it's possible to tell PHP to use the default value for a specific call. You can either move your parameters around, to have the frequently unsued params at the end (so you can call the function without passing them) or do something like this:
php:
<?
function some_function($v1='value1',$v2='value1',$v3=null){
  $v1=(is_null($v1)?'value1':$v1);
  $v2=(is_null($v2)?'value2':$v2);
  $v3=(is_null($v3)?'value3':$v3);
  echo $v1;
  echo $v2;
  echo $v3;
}
?>
That way you can pass null when you want it to use the default value.

I can't say it's clean, but it'll do the job. I'd still go with changing the parameter order if you can.

vagina rodeo
Jul 2, 2005

vulva puppets, that's what

Alex007 posted:

I don't think it's possible to tell PHP to use the default value for a specific call. You can either move your parameters around, to have the frequently unsued params at the end (so you can call the function without passing them) or do something like this:

Ugh this is the first oversight in PHP I've found that really bothers me. The situation I'm in makes it stand out really badly. I'm adding an argument to a function and I was hoping default arguments would let me not have to find every time the function is called and update them with the new arguments. Thanks for the info.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


vagina rodeo posted:

Ugh this is the first oversight in PHP I've found that really bothers me. The situation I'm in makes it stand out really badly. I'm adding an argument to a function and I was hoping default arguments would let me not have to find every time the function is called and update them with the new arguments. Thanks for the info.

How would you do it otherwise?

Scaevolus
Apr 16, 2007

duz posted:

How would you do it otherwise?

Python handles this nicely.

I think there was a hack somewhere that did reflection to let php do keyword arguments with defaults.

MrEnigma
Aug 30, 2004

Moo!
You could get around this by making all your functions only take in an array, and then parsing out the variables from that OR

You can also use func_get_arg and then have no defined arguments required. This means you'll need to do all your own checking, but it gives you a lot more control.

For code that's already written I just make sure to pass in the would be default values to them. Like instead of passing $show_query or nothing at all, pass in false. It's kind of annoying, but you're probably not going to ever change the defaults the function anyways. And if it's new, you could use one of the two solutions above.

Mine GO BOOM
Apr 18, 2002
If it isn't broken, fix it till it is.
A nice option is to instead make all defaults NULL, and do is_null() checks at the top of the function to set default values. Of course, this fails if you want to set it to NULL.

LastCaress
May 8, 2004

bonobo
Is there a simple lightweight php script (and free) that allows users to upload pictures (like gallery) that I can install in my site? Basically something that doesn't require registration or anything else, just an upload button and you're done.

functional
Feb 12, 2008

LastCaress posted:

Is there a simple lightweight php script (and free) that allows users to upload pictures (like gallery) that I can install in my site? Basically something that doesn't require registration or anything else, just an upload button and you're done.

R1ch put out a secure PHP script that did this. I don't have search enabled so I can't find it for you, but that should put you on the right track.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


functional posted:

R1ch put out a secure PHP script that did this. I don't have search enabled so I can't find it for you, but that should put you on the right track.

It's in the first post of this very thread.

LastCaress
May 8, 2004

bonobo
You're right, sorry. :\

jimbroof
Sep 4, 2003

my wife drew this for me :]
EDIT - I'm an idiot; was editing the wrong php.ini

jimbroof fucked around with this message at 21:33 on Aug 16, 2008

Zorilla
Mar 23, 2005

GOING APE SPIT

WHORENDOUS posted:

What can I do? Is there any way to troubleshoot this? None of the guides that I've looked at have helped at all. I'm ready to shoot myself in the face.

Do you have PHP error reporting turned off?

Alex007
Jul 8, 2004

What's the accepted style when it comes to code formatting in PHP, especially about curly brackets ?

Re-reading most of my code I can see three styles, each with their own advantages:

Style A- Very spaced, easy to read, but takes a lot of place:
code:
if($variable = "value")
{
  SomeFunction();
} 
else
{
  SomeOtherFunction();
}
Style B- Very compact, not too bad to read, very ugly when nested:
code:
if($variable = "value") {
  SomeFunction(); } 
else {
  SomeOtherFunction(); }
Style C- Less Compact, easy to read, looks okay when nested:
code:
if($variable = "value") {
  SomeFunction(); 
} else {
  SomeOtherFunction(); 
}
How do you do you do it ?

(Edit: Fixed indentation)

Alex007 fucked around with this message at 00:52 on Aug 17, 2008

Zorilla
Mar 23, 2005

GOING APE SPIT

Alex007 posted:

How do you do you do it ?

People have been arguing this for ages, but I've never seen any of those three used exactly as presented. The official documentation for PHP shows examples in this format:
php:
<?
if ($variable == "value") {
    some_function(); // yeah, PHP names things this way instead of with camel casing for some reason
} else {
    some_other_function();
}
?>
But this is also seen, though usually in C, C#, etc.:
php:
<?
if ($variable == "value")
{
    some_function();
}
else
{
    some_other_function();
}
?>

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Zorilla posted:

People have been arguing this for ages, but I've never seen any of those three used exactly as presented. The official documentation for PHP shows examples in this format:
php:
<?
K&R Style
?>
But this is also seen, though usually in C, C#, etc.:
php:
<?
Allman Style
?>

Yeah, the official documentation is strangely inconsistent. That said, I always use Allman style now, but used to use K&R style a few years ago. The important thing is to stick to one version in your code.

Alex007
Jul 8, 2004

Zorilla posted:

People have been arguing this for ages, but I've never seen any of those three used exactly as presented.

Sorry, the code tags hosed up my indentation, I've fixed it now.

Adbot
ADBOT LOVES YOU

Thirteenth Step
Mar 3, 2004

Im trying to learn this language in my spare time and im trying to wrap my head around sessions. Im trying to create a 4-step registration type page where I have name and address on 1, email and telephone number on 2, acceptance of terms on 3 and a final confirmation on the final page which lists everything you have inputted.

This is the code I have from the pages where information is bieng entered, this is an example of the first page;

php:
<?php 
session_start();

$_SESSION['name'] = $name;
$_SESSION['address'] = $address;
$name=$_POST['name'];
$address=$_POST['address'];

?>

<form action="two.php" method=POST>
Name: <input type=text name='name'><br>
Address: <input type=text name=address><br>
<input type=submit value="Continue..."><p>
</form>
and when I come to do the following on a later page

php:
<?php 
session_start();

echo "You entered "$_SESSION['name'];

?>

No luck. I can however recall the session variables from the SAME page. Just not other pages.

Anyone have any ideas?

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