Help me with PHP please.

ibex333

Diamond Member
Mar 26, 2005
4,094
123
106
I have 3 files. 1 HTML file, 1 PHP file and one CSS file.

When I run my index.php my "echo" statements place their output on the upper right of the webpage, but I want them right next to my labels. I want scores output to be placed next to scores, Score Total next to Score Total and so on...

I tried googling for this, but no matter how much I search, the darned thing still tells me my variable is undefined! I am completely lost. I have no idea what I am doing wrong.

Don't worry about the unfinished code for dice rolls. I will deal with that later once I understand how to properly output my results.

index.php
Code:
<!DOCTYPE html>
<html>
<head>
    <title>Loop Tester</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>

<?php
$action = filter_input(INPUT_POST, 'action');
if($action == 'process_scores'){
//ECHO "SCORES";
$scores = $_POST['scores'];
//var_dump($scores);

$score_string='';
$score_total=0;
$score_average=0;

foreach($scores as $score){
$score_string.=$score.' ';
$score_total+=$score;
}
echo $score_string . '<br>';
echo $score_total . '<br>';
$score_average = $score_total/3;
echo $score_average . '<br>'; 

}
else if ($action == 'process_rolls'){ 
    
    ECHO "ROLLS";
    
}

/*$rolls = 'number_to_roll';
$sides = 6;

$results = array();

for($i = 0; $i < $rolls; $i++) {
	$results[] = rand(1,$sides);
}

echo "Each roll:<ul><li>";
echo implode("</li><li>", $results);
echo "</li></ul>";

$total = array_sum($results);

echo "Max: $total<br>";
echo "Average: ". $total / count($results);*/










include 'loop_tester.php';
?>

    </body>
</html>


loop_tester.php
Code:
<!DOCTYPE html>

<html>
<head>
    <title>Loop Tester</title>
    <link rel="stylesheet" type="text/css" href="main.css"/>
</head>

<body>
    
<main>
    <h1>Loop Tester</h1>
    <h2>Process Scores</h2>
    
    <form action="." method="POST">
        <input type="hidden" name="action" value="process_scores">

        <label>Score 1:</label>
        <input type="text" name="scores[]" ><br>

        <label>Score 2:</label>
        <input type="text" name="scores[]" ><br>

        <label>Score 3:</label>
        <input type="text" name="scores[]" ><br>

        <label>&nbsp;</label>
        <input type="submit" value="Process Scores"><br>

        <label>Scores:</label>                      
        <span>"<?php echo $score_string; ?>" ></span><br> [b]<!-- WHY THE HECK IS THIS UNDEFINED, AND HOW CAN I GET IT TO SHOW PROPERLY? -->[/b]
        

        <label>Score Total:</label>
        <span></span><br>

        <label>Average Score:</label>
        <span><span><br>
    </form>

    <h2>Process 1000 Die Rolls</h2>
    <form action="." method="post">
        <input type="hidden" name="action" value="process_rolls">

        <label>Number to Roll:</label>
        <select name="number_to_roll">   <!--   If possible, change this to do the same with a php loop-->
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
        </select><br>

        <label>&nbsp;</label>
        <input type="submit" value="Process Rolls"><br>

        <label>Maximum Rolls:</label>
        <span></span><br>

        <label>Average Rolls:</label>
        <span></span><br>
    </form>
            
</main>
            
</body>
</html>

main.css
Code:
body {
    font-family: Arial, Helvetica, sans-serif;
}
main {
    width: 450px;
    margin: 0 auto;
    padding: 1em;
    background: white;
    border: 2px solid navy;
}
h1 {
    margin-top: 0;
    color: navy;
}
label {
    display: block;
    width: 8em;
    text-align: right;
    margin: 0 0 .5em;
    padding: 0 1em;
    float: left;
}
input, select {
    display: block;
    float: left;
    margin-bottom: .5em;
    margin-top: 0;
}
br {
    clear: left;
}
 

purbeast0

No Lifer
Sep 13, 2001
53,486
6,326
126
you said you have 1 html, 1 php, and 1 css file, however you posted 2 php files and 1 css file.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,583
4,495
75
I see. You're including the loop_tester.php file from index.php, and the variable you're looking for doesn't get passed through. It's probably a scope thing. You could try declaring the variable global in loop_tester, but that's an extra line. Or you could use the $GLOBALS array.

P.S. Seems like a better style would be if the inclusion went the other way, but maybe that's just me. And it would take a lot of reworking.
 

purbeast0

No Lifer
Sep 13, 2001
53,486
6,326
126
loop tester is a php file that only has html code in it so far.

there is PHP code in looptester where you are having the problem.

i was just unsure if you were missing something in the info you included, which is why i asked.

i'm also pretty sure you are going to have two <head>, <html>,<doctype> and <body> tags based on the way you have it written right now. it's been a long time since i've done php, but I thought the include command literally just copied the contents of the file you are including into the spot you are including it.
 
Last edited:

ibex333

Diamond Member
Mar 26, 2005
4,094
123
106
I see. You're including the loop_tester.php file from index.php, and the variable you're looking for doesn't get passed through. It's probably a scope thing. You could try declaring the variable global in loop_tester, but that's an extra line. Or you could use the $GLOBALS array.

P.S. Seems like a better style would be if the inclusion went the other way, but maybe that's just me. And it would take a lot of reworking.

Yes, that's exactly it. Are there any other ways to pass in my variables without reworking my files too much?