1
\$\begingroup\$

It works and does what is supposed what to do btw.

<?php 

ob_start();
session_start();
include("php/connect.php");

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>Welcome to albsocial</title>
    <link rel="stylesheet" href="css/main.css"/>
    <link rel="stylesheet" href="css/menubar.css"/>
    <script type="text/javascript" src="../js/analytic.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<script type="text/javascript">

    setTimeout(function() {

        $('#error_check').fadeOut('slow');

    }, 5000);

</script>
<body>

<div id="header">

    <div id="user_logged">
        </div>

            <?php
            if(isset($_SESSION['username']) && $_SESSION['username'] != ''){

                if(!isset($_GET['user'])){

                    echo "
                        <div id='ligat'>

                            <ul>
                            <li class='first'><a href='seria.php'>Seria A</a></li>
                            <li><a href='laliga.php'>La liga</a></li>
                            <li><a href='#'>Premier Liga</a></li>
                            <li><a href='#'>Bundesliga</a></li>
                            <li><a href='#'>Ligue 1</a></li>
                            </ul>

                        </div>
                    ";
                }
            }else{
                echo "";
            }

            ?>

    </div>

</div>

<div id="wrapper">

    <div id="logo">

        <a href="/">Albsocial</a>

    </div>

    <div id="login">

        <?php

            if (isset($_SESSION['username']) && $_SESSION['username'] != ''){

                $username = $_SESSION['username'];
                echo "<h4><a href='member.php?user=".$username."'>".$username."</a></h4><a href='logout.php'>LogOut</a>";

            }else{

                echo "<a href='login.php'>Login</a> <a href='#'>Register</a>";
            }

        ?>

    </div>

    <div id="menubar">

        <?php include("php/bar.php");?>

    </div>

    <div id="content_wrap">

    <div id="content_member">

        <?php

            //MARRIM USERNAME QE E VEJM NE ADRESS BARS
            if (isset($_GET['user'])){

                $username = mysql_real_escape_string($_GET['user']);

                if(ctype_alnum($username)){

                    $check = "SELECT `username` FROM user WHERE username='$username'";
                    $get = mysql_query($check)or die(mysql_error());

                    if(mysql_num_rows($get)===1){

                        $row = mysql_fetch_assoc($get);
                        $username = $row['username'];

                    }else{

                        echo "Ky profil nuk ekziston.";

                    }

                }

            }

        ?>

    <?php

    if (isset($_SESSION['username']) && $_SESSION['username'] != ''){

        if (!isset($_GET['user'])){

            //Ndeshjet e fituara ose jo
            echo "<h3>Ndeshjet e vendosura nga <b>$username</b> dhe Rezultatet:</h3><br/>";

            $matches = "SELECT * FROM match_select WHERE user_id='$username'";
            $query_match = mysql_query($matches)or die(mysql_error());

                while ($row = mysql_fetch_assoc($query_match)){

                $id = $row['match_id'];
                $liga = $row['liga'];       

                if ($row['result'] == $row['final']){

                    $hey =  "style='color: green;' ";

                    $match = "SELECT * FROM `winner` WHERE `user_id` = '$username' AND  `match_id` = '$id' AND `liga`='$liga'";
                    $matchResult = mysql_query($match)or die(mysql_error());

                    if($_POST['submit']){
                        if(mysql_num_rows($matchResult)) {
                            $error1 = "<div id='error_check'>I keni marre piket ose Nuk jeni fitues.</div>";
                        }else{
                            mysql_query("INSERT INTO winner (user_id, match_id, final, liga) VALUE ('$username','$id', '1', '$liga')");         
                            $error1 = "<div id='error_check'>Piket u shtuan ne database</div>";
                        }               
                    }

                }else if($row['final'] == ""){      
                    $hey = " style='color: #333;'"; 
                    $n = "?";
                }else{
                    $hey = " style='color: red;'";
                }

                echo "          
                    <div id='my_selection'><h4>
                        ".$home = $row['home']."
                        - 
                        ".$away = $row['away']." -  
                        ".$input = $row['result'] ."
                        </h4>
                    </div>

                    <div id='results'>
                        <h4 $hey>".$home = $row['home']."
                        -
                        ".$away = $row['away']."
                        -
                        ".$result = $row['final']." $n
                        </h4>
                    </div>

                ";

            }
            echo $error1;

            echo "
                <form action='member.php' method='post'>
                    <input type='submit' name='submit[$id]' id='match_check' value='Terhiq Piket'>
                </form>
            ";

        }

    }else{

        header("Location: index.php");
    }

    ?>

    </div>

    <?php if(!isset($_GET['user'])){

        $result = mysql_query("SELECT SUM(final) AS value_sum FROM winner WHERE user_id='$username'"); 
        $row = mysql_fetch_assoc($result); 
        $sum = $row['value_sum'];

        $resul1 = mysql_query("SELECT SUM(dummy) AS value FROM match_select WHERE user_id='$username'");
        $row1 = mysql_fetch_assoc($resul1);
        $dummy = $row1['value'];

        if($dummy['value'] == ""){

            echo "";

        }else{

            echo "

            <div id='adds'> 
                <h3>Statistikat e $username</h3>
                <br/>
                <h4 style='margin-left: 10px;'>Gjithsej keni vene: ".$dummy." ndeshje.
                <br/>           
                <br/>
                Te sakta jane: ".$sum1 = $sum1 + $sum['test_value']." ndeshje.
                <br/>
                <br/>
                Te pa sakta ose akoma skane mbaruar jane: ".$no = $dummy - $sum['test_value']." ndeshje.
                </h4>
            </div>

            ";
        }

    }

    ?>

    </div>

</div>

<div id="footer">

    <div id="footerWrapp">

        <div id="copyrights"><center>©Te gjitha te drejtat jane te rezervuara nga <a href="#">ALALA</a> , 2013</center></div>

    </div>

</div>

</body>
</html>
\$\endgroup\$
0

1 Answer 1

5
\$\begingroup\$

It was not easy, but the first step for improving your code is to split the layout from the logic:

layout.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>Welcome to albsocial</title>
    <link rel="stylesheet" href="css/main.css"/>
    <link rel="stylesheet" href="css/menubar.css"/>
    <script type="text/javascript" src="../js/analytic.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
    setTimeout(function() {
        $('#error_check').fadeOut('slow');
    }, 5000);
</script>
<body>

    <div id="header">
        <div id="user_logged">
        <!--removed div//-->
        <?php if($noUserAndLoggedIn) :?>
            <div id='ligat'>
                <ul>
                <li class='first'><a href='seria.php'>Seria A</a></li>
                <li><a href='laliga.php'>La liga</a></li>
                <li><a href='#'>Premier Liga</a></li>
                <li><a href='#'>Bundesliga</a></li>
                <li><a href='#'>Ligue 1</a></li>
                </ul>
            </div>
        <? endif ?>
        </div>
    </div>

    <div id="wrapper">
        <div id="logo"><a href="/">Albsocial</a></div>
        <div id="login">
        <?php if ($isLoggedin) :?>
            <h4><a href='member.php?user=".$username."'><?=$username?></a></h4><a href='logout.php'>LogOut</a>";
        <?php else: ?>
            <a href='login.php'>Login</a> <a href='#'>Register</a>
        <?php endif: ?>
    </div>

    <div id="menubar">
        <?php include("php/bar.php");?>
    </div>

    <div id="content_wrap">
        <div id="content_member">
        <?php if ( $unknownUser ) :?> Ky profil nuk ekziston. <?php endif ?>

        <?php if ($noUserAndLoggedIn) :?>
            <h3>Ndeshjet e vendosura nga <b>$username</b> dhe Rezultatet:</h3><br/>
            <?php foreach ($lines as $line): ?>
                <div id='my_selection'>
                    <h4><?=$line['selection']['home']?> - <?=$line['selection']['away']?> - <?=$line['selection']['result']?></h4>
                </div>
                <div id='results'>
                    <h4 style='color: <?=$line['result']==1?'green':(($line['result']==-1)?'red':'#333')?> ;'>
                        <?=$line['result']['home']?> - <?=$line['result']['away']?> - <?=$line['result']['final']?>
                        <?php if ($line['result']['uncertain']): ?>?<?php endif;?>
                    </h4>
                </div>
                <?php if ($line['error']!=null): ?>?<?=$line['error'];?>

                <form action='member.php' method='post'>
                    <input type='submit' name='submit[<?=$line['id'];?>' id='match_check' value='Terhiq Piket'>
                </form>
            <?php endforeach; ?>
        <?php endif; ?>
        </div>

        <?php if($noUser && $stats!=null):?>
                <div id='adds'> 
                    <h3>Statistikat e $username</h3>
                    <br/>
                    <h4 style='margin-left: 10px;'>Gjithsej keni vene: <?=$stats['s1']?> ndeshje.
                    <br/>           
                    <br/>
                    Te sakta jane: <?=$stats['s2']?> ndeshje.
                    <br/>
                    <br/>
                    Te pa sakta ose akoma skane mbaruar jane: <?=$stats['s3']?> ndeshje.
                    </h4>
                </div>
        <?php endif; ?>
        </div>
    </div>
    <div id="footer">
        <div id="footerWrapp">
            <div id="copyrights"><center>©Te gjitha te drejtat jane te rezervuara nga <a href="#">ALALA</a> , 2013</center></div>
        </div>
    </div>
</body>
</html>

content.php

<?
$isLoggedin=isset($_SESSION['username']) && $_SESSION['username'] != '';
if (!$isLoggedin) 
{
    header("Location: index.php");
    die();
}
$noUser=!isset($_GET['user'])
$noUserAndLoggedIn= $isLoggedin && $noUser;

$username = $isLoggedin?$_SESSION['username']:"";

$unknownUser=false;
 if (isset($_GET['user'])) {
    $username = mysql_real_escape_string($_GET['user']);
    if(ctype_alnum($username)){
        $check = "SELECT `username` FROM user WHERE username='$username'";
        $get = mysql_query($check)or die(mysql_error());

        if(mysql_num_rows($get)===1){
            $row = mysql_fetch_assoc($get);
            $username = $row['username'];
        }else{
            $unknownUser=true;
        }
    }
}

$lines=array();
if ($noUserAndLoggedIn) {
    $matches = "SELECT * FROM match_select WHERE user_id='$username'";
    $query_match = mysql_query($matches)or die(mysql_error());
    while ($row = mysql_fetch_assoc($query_match)){
        $id = $row['match_id'];
        $liga = $row['liga'];    
        $uncertain = false; 
        $error1=null;
        if ($row['result'] == $row['final']){
            $hey =  1;

            $match = "SELECT * FROM `winner` WHERE `user_id` = '$username' AND  `match_id` = '$id' AND `liga`='$liga'";
            $matchResult = mysql_query($match)or die(mysql_error());

            if($_POST['submit']){
                if(mysql_num_rows($matchResult)) {
                    $error1 = "I keni marre piket ose Nuk jeni fitues.";
                }else{
                    mysql_query("INSERT INTO winner (user_id, match_id, final, liga) VALUE ('$username','$id', '1', '$liga')");         
                    $error1 = "Piket u shtuan ne database";
                }               
            }

        }else if($row['final'] == ""){      
            $hey = 0; 
            $uncertain = true;
        }else{
            $hey = -1;
        }

        $lines[]=array('selection'=>array ('home'=>$row['home'],'away'=>$row['away'],'result'=>$row['result']),
                         'result'=>array ('home'=>$row['home'],'away'=>$row['away'],'result'=>$row['final'],'uncertain'=>$uncertain),
                         'error'=>$error1,
                         'status'=>$hey,
                         'id'=>$id);

    }
}
$stats=null;
if ($noUser) {
    $result = mysql_query("SELECT SUM(final) AS value_sum FROM winner WHERE user_id='$username'"); 
    $row = mysql_fetch_assoc($result); 
    $sum = $row['value_sum'];

    $resul1 = mysql_query("SELECT SUM(dummy) AS value FROM match_select WHERE user_id='$username'");
    $row1 = mysql_fetch_assoc($resul1);
    $dummy = $row1['value'];

    if ($dummy != ""){
        $stats=array(
            's1'=>$dummy, 
            's2'=>$sum1 + $sum['test_value'], 
            's3'=>$dummy - $sum['test_value']
        );  //better names!! but I don't understand you language
    }
}

include "layout.php";

Now you have a separate file for the representation of your data and one for the calculation. The first file is now only a simple template without any business logic. In the words of the Model-View-Controller paradigm this is the view. The model and the controller are now combined in your content.php.

In a second step you would split the content.php into two files. One is getting the data from your database (model) and the other is connecting the data from the database with then view (controller).

Unfortunately I have no time to continue this at this point. But after you have splitted this file we should concentrate on some conceptual details. But this will be far easier than now, if the responsibility are divided into this three files.

\$\endgroup\$
2
  • \$\begingroup\$ Would've given +2 if I could.. :D \$\endgroup\$
    – Abbas
    Commented Jun 21, 2013 at 12:31
  • \$\begingroup\$ Beautiful work my friend .. THanks a bunch man \$\endgroup\$
    – Love_PHP
    Commented Jun 22, 2013 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.