Loop problem

Discussion in 'Programming' started by ssimon171078, Nov 14, 2014.

  1. #1
    i need to receive a list of links but i receive another list of link how to fix this small code:
    a list that i need to receive :
    https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/1
    https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/2
    https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/3...
    i write php script:
    <?php
    $website="https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching;
    for ($Page=1;$Page<365;$Page++,$i++){
    $website =$website."/"."$i";
    echo $website."<br>";
    }
     
    ssimon171078, Nov 14, 2014 IP
  2. digdan

    digdan Active Member

    Messages:
    21
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    83
    #2
    digdan, Nov 14, 2014 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #3
    1) slow double quotes for nothing.

    2) you failed to CLOSE said quotes.

    3) You never initialize $i

    4) which seems to be a variable for nothing...

    5) You are adding to the string inside the loop, meaning ti will KEEP plastering that $i on the end over and over making the link bigger.

    6) Variable for nothing inside the loop

    7) Slow string addition on the echo for nothing.

    <?php
    
    $website = 'https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/';
    for ($page = 1; $page < 365; $Page++) echo $website, $page, '<br>';
    
    ?>
    Code (markup):
    Of course, depending on what you are doing I'm not sure you need the full URL or that it should have it's own variable either.
     
    deathshadow, Nov 15, 2014 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Irrelevant to even point out. The actual performance "hit" is not the least bit noticable. In fact, this is just petty and will never ever become an issue with noticeable performance.
     
    NetStar, Nov 16, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    If one can't be bothered to learn to use the language properly, you shouldn't be wasting everyone's time doing anything. Double quotes in PHP suck -- they ARE slower (most noticeably at the parsing level; I'll admit once you are past the parser there's no difference so bytecode cache FTMFW), if you are outputting markup you either end up using the wrong quotes or wasting time having to escape characters endlessly, they often screw up multi-line output for Christmas knows what reason (I've still not been able to figure out why it fails sometimes and other times does not in php, really strange)...

    ... and for what? The extra step of holding down shift? Seriously, is it THAT hard to NOT do something as simple as not hitting shift? Lame, lame, LAME!!!

    If we're going to correct the poor guy,we might as well correct ALL of it.

    Though thinking of corrections, typo. I have one $Page instead of $page... have I ever mentioned how much I HATE case sensitive stuff on computers? Again though, I'm a Wirth kind of guy and still think of K&R as "New kid bull".
     
    deathshadow, Nov 17, 2014 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    Your server performance issues will never be tied to the use of double quotes when outputting text. I agree with you for following the standard and doing things correctly.
     
    NetStar, Nov 17, 2014 IP
  7. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #7
    Yea, single quotes are better in PHP, and so for your own ease, you might aswell use single quotes in javascript as well, and double quotes for HTML markup. This'll save you a ton of problems if you get script-hungry in javascript too :) But it's hardly a make or break point. And once again, deathshadow could be more polite lol :)
     
    seductiveapps.com, Nov 20, 2014 IP
  8. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #8
    
    <?php
    $website='https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching'; // you missed an ending ' (or " in your original example)
    for ($page=1;$page<365;$page++,$i++){ // not $Page, i'd advise to use "hungarian notation" for *all* your variables in any language, helped by underscores (for instance if you want to turn a bunch of functions inside an object into a sub-object without the overhead of creating an actual php / javascript object of them) (or double underscores if you engage in polymorphic functions - the part after __ would be the pluginname then)
    $url = $website.'/'.$i;
    $website ='<a href="'.$url.'">'.$url.'</a>'; // in all languages you use, dont forget a space between = and the variable name ($website= : bad, $website = : good) because if you ever build lots of software and seek in all files for where a variable is set, you *have* to search for $website = and not both $website= and $website =
    echo $website.'<br/>'; // all HTML elements in HTML5 need /> not >
    }
    Code (markup):
     
    Last edited: Nov 20, 2014
    seductiveapps.com, Nov 20, 2014 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Except for the blatant non-assignment of $i, you're wrong that all elements in HTML5 needs a closing /. HTML5 is not XHTML. For instance, br and hr are stand alone elements, in no need of a closing slash.
     
    PoPSiCLe, Nov 20, 2014 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Does no harm either -- I prefer the XML style closures so I can see that the tag is being closed; but thanks to HTML 5's mouth-breathing re-re loosening of the structural rules to the point it makes SGML look like a nun with a ruler if you're using HTML 5 it doesn't matter one way or the other... unless you're really going to get upset about an extra 20 bytes in the markup, something that can usually be made up for elsewhere.

    Really though the big problem as I see it is with the ridiculous amount of code and variables for nothing people seem to want to use. Unless $i doesn't start at 0, there's no reason to even have it as a variable. All you want to echo is the links followed by a BR, why do you need ANY extra strings inside the loop?

    Let's talk line-by-line...

    $website="https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching;

    Double quotes as discussed, but it's also NOT CLOSED. It's missing the closure. Of course if these are to be links on that website, you probably shouldn't need the full URL... you only need to say the full URL if linking off-site.

    for ($Page=1;$Page<365;$Page++,$i++){

    Not sure what $i is or why you think you need it.

    $website =$website."/"."$i";

    changes the value of $website, so as it loops the output would be:

    https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/1
    https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/1/2
    https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/1/2/3
    https://www.tradebit.com/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/1/2/3/4

    Pretty sure that's NOT the desired result! Much less the double quote escaping the variable for nothing.

    echo $website."<br>";
    Code (markup):
    If all you want is to echo it, why are you playing with $website?

    Again:
    <?php
    
    $website = '/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/';
    for ($page = 1; $page < 365; $page++) echo $website, $page, '<br>';
    
    ?>
    Code (markup):
    Is probably ALL that's needed there. (Assuming the link is on-site, if off site put the URI back the way it was)... Also if $website is going to be fixed, do you even need that variable?

    <?php
    
    for ($page = 1; $page < 365; $page++)
      echo '/filesharing.php/1010-Documents-eBooks-Audio-Books-Teaching/', $page, '<br>';
    
    ?>
    Code (markup):
    Is probably all that's needed unless that $website value is being assigned far, far, far away and/or you might try to re-use this loop.

    More and more I'm seeing people make code with variables for nothing, processing inside the loop that would be better handled before the loop, string additions instead of comma delimits -- Is there some new book or tutorial out there everyone is following that is filled with such stupid coding practices? We're talking coding that would make DiBol paid by the K-LoC devs from the late '70's early '80's shudder in horror.
     
    deathshadow, Nov 20, 2014 IP