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
Evil Angry Cat
Nov 20, 2004

nbv4 posted:

is there a more elegant way to do this?:

code:
$variable = $field . "_sel";
$$variable = "checked";

When this is absolutely neccesary I go with ${$variable} = "whatever"; . I'd have to see a bigger section of your code to see if there was an easier/cleaner way around it (which there usually is).

Adbot
ADBOT LOVES YOU

Evil Angry Cat
Nov 20, 2004

Hobo posted:

This is a more general question rather than a what's wrong with this code question.

I need to adjust a login screen so that when the user enters details and submits, it logs them into both a system that being developed, as well as phpBB3. Note that while the actual database for the two is the same, the system user table is different from the phpBB3 user tables for various reasons.

Basically the way this should work is fill in form, and get logged into the system and phpBB3 at the same time, but I'm having trouble figuring out how phpBB3 handles that sort of thing.

Follow the path of the form being submitted and you'll find where phpBB is handling the request, from there just input your own login code. The action attribute of the login form will say something like "login.php?step=2" then just find that part of login.php and hey presto, you can use the login.

Evil Angry Cat
Nov 20, 2004

Hobo posted:

What do I do about the SID that phpBB3 passes? Not sure how it generates that.

If you're just hijacking the login so you can log yourself in elsewhere you don't need to follow that. You want it so you login you user to your main site as well as phpbb when they login? Well just find where phpbb logs in and add your own login code to that section, no need to follow phpBB's lead.

Evil Angry Cat
Nov 20, 2004

LastCaress posted:

Thanks, it worked, except for this line that doesn't return the clube name :\

code:
header(error("club.php?game=$game&clubid=$clubid","Thank you for joining $getclub[name]."));

Because it needs to be $getclub['name'] you've left out the quotes again. You have to use ' ' here rather than " " because of how the variable is called in the string.

Evil Angry Cat
Nov 20, 2004

sc0tty posted:

I've just started to look into PHP and I don't have a whole lot of programming experience, just some basic Java background. I'm sure there is a basic solution, so here it goes.

I have a $_SESSION['array'] with some basic data in it, and I need to remove one of the entry's completely, and effectively shift everything in the array up to replace it. How would I go about doing this?

I haven't provided a lot of detail as I'm sure there is a simple solution to this, however if I have done a lovely job of explaining my problem then I can provide some more of my code and go into it in more detail.

You want array_values()

php:
<?
$my_array = array (0 => 'Mike', 1 => 'Rob', 2 => 'Jeff', 3 => 'Tom');

//do whatever with $my_array[1] and then remove it from the array with unset($my_array[1])

$my_array = array_values($my_array);

//Produces array = ([0] => Mike, 1 => Jeff, 2 => Tom);
?>
You don't need to put the 0 => in when creating an array thats just for example purposes.

Evil Angry Cat
Nov 20, 2004

The problem I've always found with =="" and empty() checks is that accidentally a user could hit the space bar when filling in a certain field which tricks both checks and means they haven't filled a required field in. So I always use

if (str_replace(" ", "", $_POST['my_test'])=="") { error(); }

when it's something really important.

Evil Angry Cat
Nov 20, 2004

Kaluza-Klein posted:

Yes, I can get those. I will have to query for the table field names, and if I don't have a $_POST to match it then it gets set to 0.

A bit off topic, but it seems like when submitting forms values all form elements should be sent, even if they are null or blank or disabled etc. There are probably good reasons why they are not, but I have yet to notice them.

Can you not do

php:
<?php
//ice cream flavour chooser
if (isset($_POST['checkboxes']))
{
    $to_check unserialize($_POST['checkboxes']);
    foreach ($to_check as $flavour)
    {
        if (isset($_POST[$flavour]))
        {
            //doWhateverYouNeedToDo if checkbox is selected
        }
        else
        {
            //checkbox wasnt selected
        }
    }
}

$checkboxes = array ("Chocolate""Vanilla""Pastis");

echo '<form action="flavour_picker.php" method="POST" />';

foreach ($checkboxes as $checkbox)
{
    echo $checkbox.'<input type="checkbox" name="'.$checkbox.'" />';
}

echo '<input type="hidden" name="checkboxes" value="'.serialize($checkboxes).'" />';
echo '<input type="submit" name="submit" value="Submit" />';
echo '</form>';
?>

Evil Angry Cat
Nov 20, 2004

raej posted:

I can't seem to find a good tutorial on how to enter a birth date in a user registration form. I'm not sure how I should store it in my database, or how to parse it to that format.

Should I go with one input box and do a lot of parsing to figure out which of the inputs are numbers, and which are slashes or dashes, or do some sort of 3 box setup and put it all together?

I generally do three input boxes that already have MM DD YYYY in them as it takes less time to write parsing from different possible entry types to a timestamp. Use mktime() to change the input(s) you get from the form into a timestamp and then store it as a TIME (or maybe DATETIME?) in your database.

Evil Angry Cat
Nov 20, 2004

duz posted:

Why store a date as a TIME in your database? Why not store it as, I don't know, a DATE?

I've never really heard of a datestamp before have you?

Evil Angry Cat
Nov 20, 2004

duz posted:

No, which is why I said DATE.



The reason I store as a timestamp rather than using MySQLs inbuilt DATE type is because I personally find it easier to manipulate with php. Apologies, it's been so long since I used DATE that I'd forgotten it wasn't just another timestamp type.

Evil Angry Cat
Nov 20, 2004

It's the === 0.

php:
<?
function format_title($string) {
    $bit_array = array('The ' => ', The', 'A ' => ', A', 'An ' => ', An');
    
    foreach ($bit_array as $prev => $new)
    {
        if (strpos($string, $prev)!==FALSE)
        {
            $string = (strpos($string, $prev)==0) ? ltrim($string, $prev) . $new : $string;
        }
    }
    
    return $string;
}
?>
This should work properly

Evil Angry Cat
Nov 20, 2004

nbv4 posted:

Thats not quite as comprehensive as I'd like. Anyways, I found airnav.com, which is 100% comprehensive for american airports, which is about as good and I'm going to find I'm afraid.

Now, I'm wondering, whats the best way to go about mining the data from the page? I'm looking around the web, but I'm not finding much on how to actually build a web crawler... Anyone know of any good resources/tools for doing something like this?

To be honest you're best off using Google Maps. If you notice that searching "KBOS" or "LAX" or "LGW" in Google Maps brings up the airport in question it's jut matter of using the API to get the latitude and longitude (or whatever information you're looking for). Much simpler than parsing a page of tables (which the airnav.com page is) to extract data.

Evil Angry Cat
Nov 20, 2004

bt_escm posted:

This would be so much better as a regular expression
php:
<?
$phone = '2145551212';
$formattedString = preg_replace('/(\d{3})(\d{3})(\d{4})/','$1-$2-$3',$phone);
?>

Although I think considering the user is so new to php that he thought splitting a string required some sort of loop, substr() is a better method than reg exps.

Evil Angry Cat
Nov 20, 2004

You know I'm as willing to stick up for PHP as just about anyone when the going gets tough but I've just come across something that is such complete idiocy that I can't even begin to fathom why whoever wrote it is even allowed to continue existing.

I think we all accepted a long time ago that PHPs core functions were a bit loopy and zany, like a very inconsistent clown. For instance today I became annoyed once more in PHPs assumption that I never want to preserve keys in my arrays. Excuse me sir, you want to preserve keys in array_reverse()? Well you better flag it because obviously it sense to not preserve them by default!

Then today after crafting quite a handsome depthSort() function that will sort an array of arrays based on a consistent internal key and testing it using associative and non-associative keys such as:

php:
<?
$thisarr = array ('Ben' => array ('Happy' => 12, 'Sad' => 20), 'Rob' => array ('Happy' => 10, 'Sad' => 22), 'Mike' => array ('Happy' => 5, 'Sad' => 100));

$thisarr = depthSort($thisarr, "Happy");
print_r($thisarr);
?>
I was very happy with my creation, until three hours later I came to use it on an array which looks like this:


Array ( [6] => Array ( [Total] => 4 [Correct] => 2 ) [11] => Array ( [Total] => 4 [Correct] => 2 ) [10] => Array ( [Total] => 4 [Correct] => 2 ) [7] => Array ( [Total] => 4 [Correct] => 2 ) [3] => Array ( [Total] => 3 [Correct] => 2 ) [8] => Array ( [Total] => 4 [Correct] => 1 ) [1] => Array ( [Total] => 4 [Correct] => 0 ) [9] => Array ( [Total] => 3 [Correct] => 2 ) [2] => Array ( [Total] => 4 [Correct] => 3 ) [5] => Array ( [Total] => 3 [Correct] => 1 ) [4] => Array ( [Total] => 3 [Correct] => 2 ) )


After having it not work for a while and trying to figure out what the problem is, I head over to php.net to take a look at some of the internal functions that depthSort() uses to see if I can find the problem. Low and behold:

array_multisort() can be used to sort several arrays at once, or a multi-dimensional array by one or more dimensions.

Associative (string) keys will be maintained, but numeric keys will be re-indexed.


I just can't fathom why anyone thought this would be a good idea? Why is it that they've decided that numeric keys are completely useless and no would ever use them associatively? I mean there has to be a reason doesn't there? Please can someone explain to me why someone lacked such basic sense as to automatically re-index an array thats being sorted by an integer based key? Gah sometimes PHP.

Evil Angry Cat
Nov 20, 2004

functional posted:

The reason is that all PHP arrays are associative, but 'unkeyed' arrays typically rely on keyed values to determine order. It wouldn't make sense for me to create $a=Array('Z','Y','X') and have $a[0] return the same value before and after the sort.

Good point well made. If only they'd give me the option though like they do with all of their other barmy array functions I'd be happy.

Adbot
ADBOT LOVES YOU

Evil Angry Cat
Nov 20, 2004

Put ob_start(); as the first line of the page and ob_end_flush(); as the last one. Should work.

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