Okay I have list being populated and echoed out on a page
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
while($record = mysql_fetch_array($mydata)){
echo "<td>" . $record['upkeep'] . "</td>";
Now the results fluctuate depending on the number of 'mech_units' there are. What I need is to display the sum of the Upkeep of the units being displayed. Any suggestions?
You can use array_sum function
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
$upkeep = []; // Empty array to store all retreived values
while($row = mysql_fetch_array($mydata)){
echo "<td>" . $row['upkeep'] . "</td>";
$upkeep[] = $row['upkeep']; // Fill array with your values
}
echo array_sum($upkeep); // Now just coun array sum from temporary array
EDIT: I made some edit to fit your exact needs.
Related
Okay I have a list being populated and echoed out on a page.
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
while($record = mysql_fetch_array($mydata)){
echo "<td>" . $record['units'] . "</td>";
Now the results fluctuate depending on the number of 'mech_units' there are. What I need is to display how many are being displayed in the list. Any suggestions?
you can use built in function mysql_num_rows($mydata). This will give you the total number of records that are fetched.
First of all, I would suggest using mysqli.
You could declare a variable which increases by one every time you echo a 'mech_unit'.
$sql = "SELECT * FROM `game_toe` WHERE `owner`='$mech_units'";
$mydata = mysql_query($sql);
$i = 0;
while($record = mysql_fetch_array($mydata)){
$i++;
echo "<td>" . $record['units'] . "</td>";
}
echo "There are " . $i . " mech_units.";
Another option would be to use the mysql_num_rows() function.
In a table i have sums ie 1+1, im trying to make the code work the answer out and check it against the answer given by the end user.
Code
$stmt = $db->prepare("select * from exams where username = :username");
$stmt->execute(array(':username' => "$username"));
$row = $stmt->fetch();
$stmt->execute();
$question1 = $row['q1'];
foreach( $stmt as $row )
{
echo "q1<td>" . $row['q1'] . "</td>";
echo "<td>" . $row['q1a'] . "</td>";
echo "<td>Correct = " . $question1 . "</td>";
}
Table information on the row q1
type = text
the varibles
$row['q1'] = 1+1
$row['q1a'] = there answer
$question1 = the right answer
i have tried $question1 = $row['q1'];
and the output is always the sum in the table.
is there a way to echo the sum (in my table) out and find the value?
PHP will not automatically work out the answer to the given question as this is currently stored as a string, you would first need to split the string into each individual part of the sum. You may wish to read more into the explode function.
Once the string is exploded into its parts (see example below) you can then add the two individual parts together to get the answer you were originally seeking:
$questionParts = explode('+',$row['q1']);
$answer = $questionParts[0] + $questionParts[1];
I need to display mysql table columns without specifying column names in code and I also don't know total number of columns in the table.
Here is my code:
$result = mysqli_query($con,"SELECT * FROM test_table");
echo "<table border='1'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
foreach ($row as $item){
echo "<td>" . $item . "</td>";
}
echo "</tr>";
}
echo "</table>";
But output is as follows:
i.e values are repeating for each col. Please help me out.
This is because mysqli_fetch_array fetches both an associative array AND a numeric array. Try using mysqli_fetch_assoc or mysqli_fetch_row.
Alternatively, you can specify a parameter in mysqli_fetch_array, like so:
mysqli_fetch_array($result, MYSQLI_ASSOC)
Or
mysqli_fetch_array($result, MYSQLI_NUM)
Change
while($row = mysqli_fetch_array($result))
To
while($row = mysqli_fetch_row($result))
I have the code bellow, it's ok but I want to be able to use for example the 4th value extracted from the database, use it alone, not put all of them in a list, I want to be able to use the values from database individually. How do I echo them?
Edit: I was thinking to simplify things, to be able to add the values from database into one array and then extract the value I need from the array (for example the 4th - ordered by "order_id"). But how?
Right now I can only create a list with all the values one after the other..
(Sorry, I am new to this). Thank you for all your help..
<?php
include '../../h.inc.php';
$con = mysql_connect($db['host'],$db['user'],$db['passwd']);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
$result = mysql_query("SELECT * FROM options WHERE Name LIKE 'x_swift%' ORDER BY order_id");
echo "<table border='1'>
<tr>
<th>Values</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
// echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['VALUE'] . "</td>";
echo "</tr>";
$array = array(mysql_fetch_array($strict));
}
echo "</table>";
mysql_close($con);
?>
To select the value in the value column of the row where order_id is 4, use this SQL:
$query = 'select value from options where order_id = 4';
Then you can access this result in many ways. One is to get the entire result row (which in this case is just one cell) as an associative array:
if ($result = mysql_query($query)) {
$row = mysql_fetch_assoc($result);
echo 'value = ' . $row['value'];
}
You can also get the value directly:
if ($result = mysql_query($query)) {
echo 'value = ' . mysql_result($result, 'value');
}
It would just be a query like...
$result = mysql_query("SELECT * FROM options WHERE ID = 3");
mysql_fetch_row($result);
Unless Im misunderstanding you....let me know
But you really should use PDO, instead of deprecated mysql_* functions
http://php.net/manual/en/book.pdo.php
Suppose I have the following MySQL table result:
ID price
-------------
1 10
2 20
3 30
Basically what I want to accomplish is to store these values to a PHP array, and have these values displayed/echoed as a HTML table on a per row basis.
I could do something like:
if($result) {
$i = 0;
while ($row = mysql_fetch_array($result)) {
$id[$i] = $row['id'];
$price[$i] = $row['price'];
}
}
And just have those elements echo together with the HTML table.
However, I also need to have a function that allows the user to delete a row. With that mind I believe I need to have some sort of a 'key' for every row as the identifier for deletion -- a feature which multidimensional array supports.
There's nothing preventing you from using a multi dimensional array and using one of the unique values as an index:
// Store result
$data = array();
if($result) {
while ($row = mysql_fetch_array($result)) {
$data[$row['raiser_id']] = $row;
}
}
// Building table
echo "<table>";
foreach ($data as $row)
{
echo "<tr>";
echo "<td>" . $row['raiser_id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['fcr'] . "</td>";
echo "<td>" . $row['date_of_application'] . "</td>";
echo "<td>" . $row['no_of_heads'] . "</td>";
echo "<td>" . $row['place_of_farm'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Removing an entry by raiser_id
$raiser_id = 10;
if (!empty($data[$raiser_id]))
{
unset($data[$raiser_id]);
echo "Removed entry";
}
else
{
echo "No entry to remove";
}
To delete a row from the database, you have to have another PHP script and have to call it using POST method and run an SQL query in it.
If you are talking of just displaying this table, PHP has nothing to do here then - go for JavaScript.
By the time a user sees his table, there is no mysql result, no PHP array and no whole PHP running either. It's all dead long time ago. Keep that in mind.