PHP Code Review

Discussion in 'PHP' started by XT Gohan, Jul 6, 2008.

  1. #1
    Hey Guys,

    Can you look at the code and tell me why it won't compile correctly? I broke it up into several close open php tags and wrote in the HTML but I think it'd be better if I used echo statements a bit cleaner and more efficient.

    <?php
    	switch($_GET['id'])
    	{
                 case afterdark: include ("afterdark.php");  break;
                 case afterdusk: include ("afterdusk.php");  break;
    	}
    ?>
    PHP:
    I would like to place this around it:
    <div class="panel1surround"> include ("afterdark.php"); break; </div>
    Code (markup):
     
    XT Gohan, Jul 6, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    <?php
        switch($_GET['id'])
        {
                 case 'afterdark': include ("afterdark.php");  break;
                 case 'afterdusk': include ("afterdusk.php");  break;
        }
    ?>
    PHP:
    Use error_reporting(E_ALL); and tell me what errors it tells you.

    Btw, what's with this break: <div class="panel1surround"> include ("afterdark.php"); break; </div>
     
    Danltn, Jul 6, 2008 IP
  3. XT Gohan

    XT Gohan Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    I'm not sure, I'm just helping a friend fix it, I know a little PHP so I thought I could easily figure it out but I guess my knowledge was a bit more limited. Apparently, his switch statement has a great deal of cases those were just 2 out of like a hundred.
    Parse error: syntax error, unexpected '.' in /home1/theninjasuns/public_html/partials/get_id.php on line 51
    Code (markup):
    Also, I tried doing this several times but I kept getting errors:
    <?php
        switch($_GET['id'])
        {
            case 'afterdark': include ("afterdark.php");  break;
            case 'afterdusk': 
                  echo "<div class=\"panel1surround\">" . include ("afterdusk.php"); . "</div>" . break;
        }
    ?>
    PHP:
     
    XT Gohan, Jul 6, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    You have an unwanted semi-colon.

    echo '<div class="panel1surround">' . include ("afterdusk.php") . '</div>'; break;

    p.s. Use SINGLE quotes, will save you a lot of backslashes, speed and headaches.
     
    Danltn, Jul 6, 2008 IP
  5. XT Gohan

    XT Gohan Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    OMG! Im so blind LOL.

    Thanks alot man, I really appreciate that =)
     
    XT Gohan, Jul 6, 2008 IP
  6. XT Gohan

    XT Gohan Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    $imgtotal = 8
    $imgcount = 1
    
    do {
        for ($count = 0; $count < $imgtotal; $count++) {
    	if ($count == 4) || ($count == 8) || ($count == 12) {
    	  echo '<tr>';
    	  echo '<td class="ict"><a href="media/wallpapers/wall_' . $imgcount . '.jpg"><img src="media/wallpapers/th_wall_' . $imgcount . '.jpg" border="0"></a></td>';
    	  echo '</tr>';
              $imgcount++;
    	} else {
              echo '<td class="ict"><a href="media/wallpapers/wall_' . $imgcount . '.jpg"><img src="media/wallpapers/th_wall_' . $imgcount . '.jpg" border="0"></a></td>';
    	  $imgcount++;
    	}
    } while ($imgcount <= $imgtotal);
    PHP:
    For some reason it doesn't compile at all. I'm not sure where I made the mistake but I've been at for about an hour, maybe you guys can see something I'm overlooking.

    My main objective is that I'm making a table using images but I want it so that the table rows (<tr>) would only come in multiples of 4's (4, 8, 12, 16 and so forth) so I can have 4 images in a row and then start a new row. However, it seems that I'm not writing the code correctly, can you guys tell me where to go from here?

    I'd really appreciate it and thank you in advance.
     
    XT Gohan, Jul 13, 2008 IP
  7. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #7
    First, you forgot semicolons after :

    $imgtotal = 8
    $imgcount = 1
     
    xrvel, Jul 14, 2008 IP
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #8
    I don't quite understand what you want exactly...

    Here's some code I whipped up, run it and tell me if that's what you mean or not, pretty confused :p

    Dan

    <?php
    
    defined('NL') or define('NL', "\n");
    defined('TAB') or define('TAB', "\t");
    
    $imgtotal = 20;
    $perline = 4;
    $imgcount = 1;
    
    echo '<table>' . NL . '<tr>' . NL;
    while ( $imgcount <= $imgtotal )
    {
        if ( $imgcount % $perline == 0 and $imgcount != $imgtotal )
        {
            /* It's a multiple of 4 */
            /* Do some stuff here */
            echo TAB . '<td>' . $imgcount . '</td>' . NL . '</tr>' . NL . '<tr>' . NL;
        }
        else
        {
            /* It isn't */
            /* Do some different stuff here */
            echo TAB . '<td>' . $imgcount . '</td>' . NL;
        }
        ++$imgcount;
    }
    echo '</tr>' . NL . '</table>';
    
    ?>
    PHP:
     
    Danltn, Jul 14, 2008 IP
  9. XT Gohan

    XT Gohan Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #9
    Well basically, I was trying to create a table that would add rows after four cells. But the could you wrote works extremely well, however, I'm not too php savvy so I'm not really understanding the first part of your code, can you explain to me what this part means?
    
    defined('NL') or define('NL', "\n");
    defined('TAB') or define('TAB', "\t");
    PHP:
    And thanks for taking the time and helping me out. I am really beginning to get the hang of this stuff.
     
    XT Gohan, Jul 14, 2008 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Danltn, Jul 14, 2008 IP
  11. XT Gohan

    XT Gohan Greenhorn

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #11
    Thanks a whole lot Danltn. I've been able to progress with PHP to quite a degree and I've learned a good deal in such a short while. However, I was wondering if you (or anyone else) could help me out with this problem I'm having:
    I recently started using an e-mail feedback form but apparently my scripts are conflicting in some manner. Can anyone tell me where I am going wrong here and if possible, is there a different approach to writing out this code.
    <?php
    
    // load the variables form address bar
    $subject = $_REQUEST["subject"];
    $message = $_REQUEST["message"];
    $from = $_REQUEST["from"];
    $verif_box = $_REQUEST["verif_box"];
    
    // remove the backslashes that normally appears when entering " or '
    $message = stripslashes($message); 
    $subject = stripslashes($subject); 
    $from = stripslashes($from); 
    
    // check to see if verificaton code was correct
    if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
    	// if verification code was correct send the message and show this page
    	mail("webmaster@theninja.com", 'Online Form: '.$subject, $_SERVER['REMOTE_ADDR']."\n\n".$message, "From: $from");
    	// delete the cookie so it cannot sent again by refreshing this page
    	setcookie('tntcon','');
    } else {
    	// if verification code was incorrect then return to contact page and show error
    	header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true");
    	exit;
    }
    ?>
    PHP:
    Because I get the errors in the following quotation. And line 22 is referring to: header("Location:".$_SERVER['HTTP_REFERER']."?subject=$subject&from=$from&message=$message&wrong_code=true"); right after the } else {.

    Is it possible to rewrite this in a way it doesn't screw with my original headers. The truth of the matter is that I'm using php include() to use this file as a partial on to my main index page. So I believe it conflicts because the headers are being stated twice but I'm not sure what to do from here. So if you guys can help me out, I'd be forever in your debt?
     
    XT Gohan, Aug 9, 2008 IP