/    Sign up×
Community /Pin to ProfileBookmark

Form Elements With Same Name

I’m new to php and am confused as to how to make input boxes that have the same name ONE array, I tried putting brackets after the name but to no avail. [URL=”http://userpages.umbc.edu/~ofek1/gpa.html”]http://userpages.umbc.edu/~ofek1/gpa.html[/URL]

report.php:

[CODE]<html>
<head>
</head>
<body>

<?php

$credits = $_POST[‘credits’];
$points = $_POST[‘points’];
$semester = $_POST[‘sem’];

/****************************************************************************
** TotalPoints – Totals the number of quality points earned
** Input – None
** Output – Total quality points earned
****************************************************************************/

function TotalPoints()
{

$numCredits = $credits;
$numPoints = $points;
$totalPoints = 0;

for ($i = 0; $i < count($numCredits); $i++)
{

$totalPoints = $totalPoints + ($numCredits[i] * $numPoints[i]);

}

echo $totalPoints;

}

/****************************************************************************
** TotalCredits – Totals the number of credits taken
** Input – None
** Output – Total credits taken
****************************************************************************/

function TotalCredits()
{

$numCredits = $credits;
$numPoints = $points;
$totalCredits = 0;

for ($i = 0; $i < count($numCredits); $i++)
{

$totalCredits = $totalCredits + $numCredits[i];

}

echo $totalCredits;

}

/****************************************************************************
** CalculateGPA – Divides total quality points by number of credits taken
** Input – None
** Output – Calculated GPA
****************************************************************************/

function CalculateGPA()
{

$finalGPA = TotalPoints() / TotalCredits();
$finalGPA = floatval($finalGPA);

echo $finalGPA;

}

/****************************************************************************
** GenerateGradeReport – Displays the grade report to the user
** Input – Semester
** Output – None
****************************************************************************/

function GenerateGradeReport($semester)
{

$finalGPA = CalculateGPA();
$totalCredits = TotalCredits();
$totalPoints = TotalPoints();

echo “GPA report for ” . $semester . “<br /><br />”;
echo “Credits taken: ” . $totalCredits . “<br /><br />”;
echo “Quality points earned: ” . $totalPoints . “<br /><br />”;
echo “GPA for ” . $semester . “: ” . number_format($finalGPA, 3) . “<br /><br />”;

}

GenerateGradeReport($semester);

?>

</body>
</html>[/CODE]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 15.2010 — Brackets and foreach[CODE]<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>

</head>
<body>
<?php
if (isset($_POST['samename'])) {
foreach ( $_POST['credit'] as $key => $value ) {
print $key . " = " . $value . "<br>";
}
}
?>
<form action="" method="post" name="form1">
<input type="text" name="credit[]" value="25">
<input type="text" name="credit[]" value="50">
<input type="text" name="credit[]" value="75">
<button type="submit" name="samename">submit</button>
</form>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@OfekmeisterauthorSep 15.2010 — How do I pass an array pointer to a function?
[CODE]<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>

<?php

if (isset($_POST['samename']))
{

function CreditArray($key)
{

$creditCount = 0;

foreach ($_POST['credits'] as $key => $value)
{

$creditCount = $value;

}

return $creditCount;

}

$points = 0;

foreach ($_POST['points'] as $key => $value)
{

$points = $points + ($value * CreditArray($key));

}

echo $points;

}

?>

<form action="" method="post" name="form1">
<input type="text" name="credits[]" value="2">
<input type="text" name="points[]" value="7">
<input type="text" name="credits[]" value="5">
<input type="text" name="points[]" value="5">
<button type="submit" name="samename">submit</button>
</form>

</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@OfekmeisterauthorSep 16.2010 — Oh btw Fang, my above code doesn't work.
Copy linkTweet thisAlerts:
@NogDogSep 16.2010 — The array key in the if() should be the same as the field (probably fang changed the key in the rest of the code and forgot to change it there), e.g.:
[code=php]
$credits = 0;
$points = 0;

if(isset($_POST['credits']) and is_array($_POST['credits'])) {
$credits = array_sum($_POST['credits']);
}
else {
// whatever you want to do if nothing received from form for 'credits'
}

if(isset($_POST['points']) && is_array($_POST['points'])) {
$points = array_sum($_POST['points']);
}
else {
// whatever you want to do if 'points' not received from form
}

// do whatever you want now with $credits and $points . . .
?>
[/code]
Copy linkTweet thisAlerts:
@OfekmeisterauthorSep 16.2010 — array_sum() is a handy built-in function for the total credits, thanks for that! But in order to get $totalPoints you take the credits a course is worth multiplied by the points you earned for it and THAT number is added to totalPoints.

The JavaScript code was:
[CODE]function TotalPoints()

{

var numCredits = document.getElementsByName('credits');
var numPoints = document.getElementsByName('points');
var totalPoints = 0;

for (var i = 0; i < numCredits.length; i++)
{

totalPoints = totalPoints + (parseInt(numCredits[i].value) * parseInt(numPoints[i].value));

}

return totalPoints;

}[/CODE]

How would that be done in PHP? It's not working here http://userpages.umbc.edu/~ofek1/s.php:
[CODE]<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>

<?php

if (isset($_POST['samename']) && is_array($_POST['credits']) && is_array($_POST['points']))
{

function CreditArray($key)
{

$creditCount = 0;

foreach ($_POST['credits'] as $key => $value)
{

$creditCount = $value;

}

return $creditCount;

}

$points = 0;

foreach ($_POST['points'] as $key => $value)
{

$points = $points + ($value * CreditArray($key));

}

echo $points;

}

?>

<form action="" method="post" name="form1">
<input type="text" name="credits[]" value="2">
<input type="text" name="points[]" value="7">
<input type="text" name="credits[]" value="5">
<input type="text" name="points[]" value="5">
<button type="submit" name="samename">submit</button>
</form>

</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@NogDogSep 16.2010 — You could simply add this after the two array sums are done:
[code=php]
$totalPoints = $credits * $points;
[/code]

?
Copy linkTweet thisAlerts:
@OfekmeisterauthorSep 16.2010 — That's not the same ?
Copy linkTweet thisAlerts:
@NogDogSep 16.2010 — That's not the same ?[/QUOTE]

You're right, it's only the same if the same point value is used for each. How about...
[code=php]
<?php
// test data:
$_POST = array(
'credits' => range(2, 6, 2),
'points' => range(7, 3, -2)
);
echo "<pre>".print_r($_POST,1)."</pre>n";
// process it:
$totalPoints = 0;
reset($_POST['points']);
if(count($_POST['credits']) == count($_POST['points'])) {
foreach($_POST['credits'] as $credits) {
$totalPoints += $credits * current($_POST['points']);
next($_POST['points']);
}
}
// result:
echo $totalPoints;
[/code]
Copy linkTweet thisAlerts:
@OfekmeisterauthorSep 17.2010 — Hahaha, sorry Fang, NogDog. For loops DO work on arrays in PHP, but:
[CODE]function TotalPoints()

{

$numCredits = $_POST['credits'];
$numPoints = $_POST['points'];
$totalPoints = 0;

for ($i = 0; $i < count($numCredits); $i++)
{

$totalPoints = $totalPoints + ($numCredits[i] * $numPoints[i]);

}

echo $totalPoints;

}
[/CODE]


I forgot to reference the i's as variables with "$".

Thanks. ?

http://userpages.umbc.edu/~ofek1/gpa.php
Copy linkTweet thisAlerts:
@NogDogSep 17.2010 — Hahaha, sorry Fang, NogDog. For loops DO work on arrays in PHP....[/quote]

They work, as long as you are sure the array will start with a known index, and that all of the elements are sequentially numbered. Foreach(), on the other hand, always works regardless of whether the array is sequentially indexed or not -- or even an associative array. ?
×

Success!

Help @Ofekmeister spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...