PHP help needed
New on LowEndTalk? Please read our 'Community Rules' by clicking on it in the right menu!

PHP help needed

nexusrainnexusrain Member
edited August 2015 in Help

Hi,

working on a new project since some days but I now stuck at some point.

E.g., let's say I've got this string:</p> <pre>Name,Birthdate,Address,eMail,Phone<|>Name2,Birthday2,Address2,eMail2,Phone2

How would I get this string in a table like</p> <pre> Name | Birthdate | Address | eMail | Phone Name2 | Birthdate2 | Address2 | eMail2 | Phone2

I know how to split the string at defined seperators with explode and how to create a table in HTML but not how to go on with the for function.

Would also be interested in how to get the table alphabetically sorted according to the word in the first column.

Thank you! :)

nexusrain

P.S.: I do know that LET is not StackOverflow but as developers are around here as well - why not try to ask here before creating an account at SO f.e. :p

Comments

  • perennateperennate Member, Provider
    edited August 2015

    Here's an example of for function:

    <?php
    $array = array(array('name' => 'b', 'email' => '[email protected]'), array('name' => 'c', 'email' => '[email protected]'));
    foreach($array as $el) {
        echo "{$el['name']}{$el['email']}";
    }

    So this uses foreach loop, which loops through each element of the $array, and on each iteration the current element is stored in $el (which the statements inside the loop can interact with).

    You can also use a for loop with iterator, like this:

    <?php
    $array = array(array('name' => 'b', 'email' => '[email protected]'), array('name' => 'c', 'email' => '[email protected]'));
    for($i = 0; $i < count($array); $i++) {
        echo "{$array[$i]['name']}{$array[$i]['email']}";
    }

    Here, the for loop executes the statements inside the brackets with $i = 0 first, then with $i = 1, and so on up until $i = count($array) - 1 (the length of the array minus one).

    Note: I'm using the "{$x}" string formatting style here, you can use whatever you want. The ideal way would be to have templates separated so that you can have a template system that escapes all the variables to prevent XSS, but I assume you're just looking for simple script.

    Thanked by 1nexusrain
  • perennateperennate Member, Provider

    There's a bunch of sort functions in PHP, and they all have complicated names, it's one of the reasons PHP is shit (other languages usually have better interface, e.g. one sort function with optional order and optional custom sort function). Pretty much all developers have to look up which sort function they want here: http://php.net/manual/en/array.sorting.php

    For this you probably want usort, which allows a custom comparison function. http://php.net/manual/en/function.usort.php has some examples that should help you.

    Thanked by 1nexusrain
  • @perennate Thank you for your fast and detailed answer, much appreciated! This "project" is going to be something for the LE community (I don't need this script to get details of a person in a table, was just an example), if you're interested I'll send you an invitation code to the closed beta as soon as everything's working more or less stable. :)

  • <?php
    $string = "Name,Birthdate,Address,eMail,Phone<|>Name3,Birthday3,Address3,eMail3,Phone3<|>Name2,Birthday2,Address2,eMail2,Phone2";
    $arrayRecords = explode("<|>",$string);//convert to array
    if(is_array($arrayRecords))
    {
        //Loop through the array records
        for($i = 0; $i < count($arrayRecords); $i++)
        {
            //Convert comma seperated values to array so we can sort them later
            $arrayRecords[$i] = explode(",",$arrayRecords[$i]);
        }
    
        //Display Unsorted Records
        print_r($arrayRecords);
    
        //Sort Records by first element ASC
        array_multisort($arrayRecords, SORT_ASC);
    
        //Display Sorted Records
        print_r($arrayRecords);
    }
    
  • nexusrainnexusrain Member
    edited August 2015

    @Hybrid Thank you for the sorting part! The thing with the offer for beta invention is for you as well. :)

    But @perennate and Hybrid, I can't find the point where I can start a new row in the html table in your code (so where a row ends because of the "<|>" seperator), where is it? (I don't say it's not there, but as you can see not really a pro in php so that will be the reason why I can't find this point)

  • HybridHybrid Member
    edited August 2015

    Can't write html tags in here.

    check: http://pastebin.com/twT7T2mE

    Thanked by 1nexusrain
  • JonchunJonchun Member, Provider
    edited August 2015

    Just put them in

    <tr> tags

  • hostnoobhostnoob Member
    edited August 2015

    @Hybrid said:

    > <?php
    > $string = "Name,Birthdate,Address,eMail,Phone<|>Name3,Birthday3,Address3,eMail3,Phone3<|>Name2,Birthday2,Address2,eMail2,Phone2";
    > $arrayRecords = explode("<|>",$string);//convert to array
    > if(is_array($arrayRecords))
    > {
    >   //Loop through the array records
    >   for($i = 0; $i < count($arrayRecords); $i++)
    >   {
    >       //Convert comma seperated values to array so we can sort them later
    >       $arrayRecords[$i] = explode(",",$arrayRecords[$i]);
    >   }
    > 
    >   //Display Unsorted Records
    >   print_r($arrayRecords);
    > 
    >   //Sort Records by first element ASC
    >   array_multisort($arrayRecords, SORT_ASC);
    > 
    >   //Display Sorted Records
    >   print_r($arrayRecords);
    > }
    > 

    Just a little tip.

    Instead of using something like for($i = 0; $i < count($arrayRecords); $i++) it's better to use

    $recordsCount = count($arrayRecords);
    for($i = 0; $i < $recordsCount; $i++)

    That way it only has to count the array once. Of course it probably doesn't matter in this example, but if you're processing arrays with thousands of results it can make a big difference.

    Thanked by 1nexusrain

    Favourite host in general: Ramnode (affiliate link)
    Favourite host for hourly billing/custom ISOs: Vultr (affiliate link)

  • @Hybrid said:
    Can't write html tags in here.

    check: http://pastebin.com/twT7T2mE

    Awesome, the $record[this] is what I was looking for. Didn't test it yet but I'm not missing something in your code. Thanks a lot, now the development of that part can go on. :)

    @hostnoob Thanks, I'll put this in my code as well.

    Thanked by 1Hybrid
  • perennateperennate Member, Provider

    hostnoob said: That way it only has to count the array once. Of course it probably doesn't matter in this example, but if you're processing arrays with thousands of results it can make a big difference.

    Compilers and interpreters do a lot behind the scenes. There's no way to be sure that one way is faster than the other. Also the count operation (which just accesses a variable stored with the array) is going to be negligible compared to the time it takes to process a record.

    In fact I tested this on my machine with PHP 5.5.9 and there is no difference at all in the speed:

    <?php
    $array = array();
    for($i = 0; $i < 1000000; $i++) {
            $array[] = $i;
    }
    $tstart = microtime(true);
    $x = 0;
    //$c = count($array);
    for($i = 0; $i < count($array); $i++) { // versus $i < $c
            $x |= $array[$i];
    }
    echo $x;
    $tend = microtime(true);
    echo ($tend - $tstart) . "\n";
    
    ?>

    Adjusting your code in ways like this for perceived performance gains is silly and counterproductive. If performance is a top priority, you should not be using a high level language like PHP in the first place. The most expensive item here is your time, so you should use whatever style you like best and stick with it; defining extra variables that are only used once generally makes the code more complicated and is not good style.

    Thanked by 1Jonchun
  • @perennate

    Thanks for sharing. I just thought it's making sense that the calculation of the array length for every element will take more time - it may not speed things up much when it's calculated once and stored in a variable, but it won't be bad as well, so I put it in my code.

  • perennateperennate Member, Provider
    edited August 2015

    nexusrain said: Thanks for sharing. I just thought it's making sense that the calculation of the array length for every element will take more time - it may not speed things up much when it's calculated once and stored in a variable, but it won't be bad as well, so I put it in my code.

    It doesn't speed things up at all, I tested it. It's bad since it adds unnecessary code, in a large project too much of stuff like this makes the code more difficult to maintain; your time and the quality of the code is worth more than blindly trying to squeeze performance gains in a high-level language.

Sign In or Register to comment.