EDIT: I have successfully been able to calculate the value I was trying to get, but instead of calculating that value for each row, it is just calculating it once and posting that value everywhere. How do I make it recalculate for each row using the code I have?
Picture: http://img515.imageshack.us/img515/9064/example2w.png
New Code:
<html>
<head>
<title>PHP-MySQL Project 4</title>
<div align="center">
<p>
PHP-MySQL Project 4
<br/>
By: Ryan Strouse
</p>
</div>
</head>
<body bgcolor="#99FFFF">
<?php
$DBName = "surveys";
$DBConnect = #mysqli_connect("localhost", "students", "password")
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
if (!$DBConnect)
{
echo "<p> The database server is not available.</p>";
}
else
{
echo "<p> Successfully connected to the database $DBName</p>";
}
mysqli_select_db($DBConnect, $DBName);
echo "<p>Database -'$DBName'- found</p>";
$SQLstring = "SELECT * FROM surveys WHERE surveyCode = 'GEI001'";
$QueryResult = #mysqli_query($DBConnect, $SQLstring);
echo $SQLstring;
$row = mysqli_fetch_assoc($QueryResult);
$count_surveys = $row['surveyResponses'];
echo "<p>Total Responses: $count_surveys</p>";
$SQLstring2 = "SELECT * FROM results WHERE surveyCode = 'GEI001'";
$QueryResult2 = #mysqli_query($DBConnect, $SQLstring2);
echo $SQLstring2;
echo "<br/>";
$Row = mysqli_fetch_assoc($QueryResult2);
$SQLstring3 = "SELECT * FROM surveys, results";
$QueryResult3 = #mysqli_query($DBConnect, $SQLstring3);
$fetchrow = mysqli_fetch_assoc($QueryResult3);
$result_amount = (($fetchrow['resultResponses'] / $fetchrow['surveyResponses']) * 100);
echo "<table>";
echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
do {
echo "<tr><td>{$Row['resultDescription']}</td>";
echo "<td>{$Row['resultResponses']}</td>";
echo "<td>$result_amount</td></tr>";
$Row = mysqli_fetch_assoc($QueryResult3);
} while ($Row);
echo "</table>";
?>
<center>
<h3>Return To Main Page</h3>
<h3>Return to Menu</h3>
</center>
</body>
<footer>
<div align="center">
© Copyright Ryan Strouse ©
</div>
</footer>
</html>
I have two database tables and I am successfully pulling in column data into a table. The third cell of the table I would like to calculate a percentage out of some of the columns from the database. I'm not sure how to code this... I've tried to come up with something in the SELECT statement from another thread I found with no luck.
Here is a picture of the query I'm trying to get to work: http://img696.imageshack.us/img696/3862/examplegw.png
<html>
<head>
<title>PHP-MySQL Project 4</title>
</head>
<body bgcolor="#99FFFF">
<?php
$DBName = "surveys";
$DBConnect = #mysqli_connect("localhost", "students", "password")
Or die("<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>";
if (!$DBConnect)
{
echo "<p> The database server is not available.</p>";
}
else
{
echo "<p> Successfully connected to the database $DBName</p>";
}
mysqli_select_db($DBConnect, $DBName);
echo "<p>Database -'$DBName'- found</p>";
$SQLstring = "SELECT * FROM surveys WHERE surveyCode = 'GEI001'";
$QueryResult = #mysqli_query($DBConnect, $SQLstring);
echo $SQLstring;
$row = mysqli_fetch_assoc($QueryResult);
$count_surveys = $row['surveyResponses'];
echo "<p>Total Responses: $count_surveys</p>";
$SQLstring2 = "SELECT * FROM results WHERE surveyCode = 'GEI001'";
$QueryResult2 = #mysqli_query($DBConnect, $SQLstring2);
echo $SQLstring2;
echo "<br/>";
$Row = mysqli_fetch_assoc($QueryResult2);
//this is where I am trying to calculate the value and then below it display in table
//cell # 3
$SQLstring3 = "SELECT *,((resultResponses/surveyResponses)*100) AS AMOUNT FROM surveys, results";
$QueryResult3 = #mysqli_query($DBConnect, $SQLstring3);
do {
echo "<table>";
echo "<tr><th>Commercial</th> <th>Views</th> <th>Percentage</th></tr>";
echo "<tr><td>{$Row['resultDescription']}</td>";
echo "<td>{$Row['resultResponses']}</td>";
echo "<td>$QueryResult3</td></tr>";
$Row = mysqli_fetch_assoc($QueryResult);
} while ($Row);
echo "</table>";
?>
<center>
<h3>Return To Main Page</h3>
<h3>Return to Menu</h3>
</center>
</body>
<footer>
</footer>
</html>
IF I understand what you are asking, you're trying probably to calculate the percentage of a value that you can find in a MySQL query's results. If that is so, then I'd use the function mysql_num_rows to get the total of the records and then in a while and if I'd have a counter of how many times I meet that value.
Then you just do simple math, for example:
result = (100 * counter) / mysql_num_rows
and have a percentage. Then you just echo the result wherever you want! :)
I hope I have understood your question correctly!
$result = mysql_query("SELECT yourRow FROM yourTable");
$aArray = array();
$cont=0;
while($col = mysql_fetch_assoc($result){
$aArray[$cont] = $col['yourRow'];
cont++;
}
That should give you a workable array for you to make your math with it and then echo it. Cheers
Related
I have two Mysql tables "addcoupons" with one column(couponvalue) and two rows with values 50 and 100. Another table "paytoget" with one column(usercouponinhand).
I been trying to get all the rows in table-1 to a html page and pass the particular id of the row when user click on its value to anther php page where it inserts it in table-2. But unfortunately i have missed something which causing it to print id of first row irrespective of which value user clicks.
My code goes as:
Page 1 (user interaction page)
<br>
<?php
$sql2 = "SELECT * FROM addcoupons WHERE customernumber = '$mobile' ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
$index = 0;
while($row2 = $result2->fetch_assoc()) {
$index++;
?>
<form action='usercouponadd.php?id=<?php echo $row2['id'];?>' method="post" id="usercouponadd">
<div>
<h4 class="amount" form="usercouponadd"> <strong>Rs. <?php echo ($row2["couponvalue"]); ?></strong></h4>
<button class="addbutton" form="usercouponadd" type="submit"><span> <strong>Add</strong> </span></button>
</div>
<hr class="line" align="left">
<?php }
} else {
echo "None";
}
?>
</div>
The second php page (insert into table-2)
$mobile = $_SESSION['mobile'];
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql2 = "SELECT * FROM addcoupons WHERE id = " .$_GET["id"];
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
$index = 0;
while($row2 = $result2->fetch_assoc()) {
$index++;
$usercouponinhand = $row2["couponvalue"];
$sql = "INSERT INTO userpaytoget (usercouponinhand, mobile, date, date2)
VALUES ('$usercouponinhand', '$mobile', '$date', '$date2')";
if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("coupon has been successfully added")';
echo '</script>';
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
}
} else {
echo " ";
}
$conn->close();
Table-1 "addcoupons"
couponvalue
50
100
Table-2 "paytoget"
usercouponinhand
xx
xx
Any Help is Appreciated....
I guess i figured out the answer for "my" problem.
I just removed the form tag since i m not submitting any data from user, and added that addcoupon.php link to anchor link. it worked talking the particular id which user clicks..
Code goes like this::
<div>
<h4 class="amount" form="usercouponadd"> <strong>Rs. <?php echo ($row2["couponvalue"]); ?></strong></h4>
<a href="usercouponadd.php?id=<?php echo $row2['id'];?>" class="addbutton" >Add</a>
</div>
Thanks to all who took their time to give this problem a glance...
The following PHP program is to search for a student number in database and display the details if found or give a message if it does not exist.
<html>
<body>
<?php
$sno=$_POST['studNo'];
$connection = mysql_connect("localhost", "root", "")
or die("couldn't connect to the server");
$db = mysql_select_db("student", $connection)
or die("<b>connection fails");
$query = "select * from performance where Number = '$sno'";
if($result = mysql_query($query))
{
echo "<table border = 1 align = center>";
echo "<tr>";
echo "<th>Number<th>Name<th>Address<th>Mobile Number";
echo "</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<th>",$row['Number'],"</th>";
echo "<th>",$row['Name'],"</th>";
echo "<th>",$row['Address'],"</th>";
echo "<th>",$row['MobileNo'],"</th>";
echo "</tr>";
}
echo "</table>";
echo "The student data updated";
}
else
{
echo "<b>Customer number does not exist";
}
mysql_close($connection);
?>
</body>
</html>
If i search for a number which does not exist the else block runs. When i give a number which exists then the if block runs but the while loop does not run. Can anybody help me out? Database field names are correct.
mysql_query() only returns false if there's an error in the query. Not finding any matching rows is not an error. To tell if any rows were found use mysql_num_rows().
$result = mysql_query($query) or die("Query error: " . mysql_error());
if (mysql_num_rows($result) > 0) {
...
} else {
echo "<b>Customer number does not exist</b>";
}
I'm starting to build a very simple product display system, mainly to build my own skills, but also for use on my website for work.
List.php
<html>
<head>
<title>Retrieve data from the database</title>
<?php
$username='';
$password='';
$database='';
?>
</head>
<body>
<ul>
<?php
// Connect to database server
mysql_connect(localhost,$username,$password);
// Select database
#mysql_select_db($database) or die( 'Unable to select database');
// SQL query
$sql = "SELECT * FROM products WHERE category = 30";
// Execute the query (the recordset $rs contains the result)
$result = mysql_query($sql);
// Loop the recordset $rs
while($row = mysql_fetch_array($result)) {
// Name of the person
$strName = $row['make'];
// Create a link to person.php with the id-value in the URL
$strLink = "<a href = 'product.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
// List link
echo "<li>" . $strLink . "</li>";
}
// Close the database connection
mysql_close();
?>
</ul>
</body>
</html>
Product.php
<html>
<head>
<title>Retrieve data from database</title>
</head>
<body>
<?php
$username="";
$password="";
$database="";
// Connect to database server
mysql_connect(localhost,$username,$password);
// Select database
#mysql_select_db($database) or die( "Unable to select database");
// Get data from the database depending on the value of the id in the URL
$sql = mysql_query('SELECT * FROM products WHERE ID=' . $_GET["ID"]);
$result = mysql_query($sql);
if(!$result)
die(mysql_error());
// Loop the recordset
while($row = mysql_fetch_array($result)) {
// Write the data of the product
echo $row['category'];
echo "<p>";
echo $row["make"];
echo "<p>";
echo $row["description"];
echo "<p>";
echo $row["picture"];
}
// Close the database connection
mysql_close();
?>
<p>Return to the list</p>
</body>
</html>
Can be seen messing up HERE
If someone can help get this working for me I'd be very grateful!
Try this way, but not fully sure about this stubborn issue. You can try by changing the case of all keys of the $_GET array to lowercase using the array_change_key_case()
$get = array_change_key_case($_GET);
$id = $get['id'];
$sql = mysql_query('SELECT * FROM products WHERE ID=' . $id);
NB: Make first sure SELECT * FROM products WHERE ID=your_row_id return results or not?
I turned out that the link that the page 'list.php' was generating was incorrectly formatted.
Changed this:
$strLink = "<a href = 'product.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
To
$strLink = "<a href = 'product.php?id=".$row['ID']."'>" . $strName . "</a>";
And it's now working exactly as it should!
I have a simple query to pull all the data from a table but I only need display the value once no matter how many times its in the table. Right now I get the following results:
Material in Que
9231500
9231500
9231500
Grp1298
Grp1298
6251752
6251752
What my desired result is to get:
Material in Que
9231500
6251752
Grp1298
How Do I sort out repeating numbers or Text from my query results?
For the array with column 3 from my table $row[2]
<?php session_start(); ?>
<html>
<head>
<basefont face="Arial">
<title>Material in Testing que</title>
</head>
<body>
<?php
// set database server access variables:
include('db.php');
// open connection
$connection = mysqli_connect($host, $user, $pass, $db);
if (mysqli_connect_errno($connection)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// create query
$query = "SELECT * FROM testingqa1160";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
echo "Material in Testing Que";
echo "<br>";
while($row = mysqli_fetch_row($result)) {
echo $row[2];
echo "<br>";
echo " ";
}
}
else {
// print status message
echo "<center>";
echo " Que Empty </font>";
echo "</center>";
}
mysqli_free_result($result);
// close connection
mysqli_close($connection);
?>
</body>
</html>
Use MySQL's DISTINCT in your query:
$query = "SELECT DISTINCT colname FROM testingqa1160";
you can us group by clause
$query = "SELECT * FROM testingqa1160 GROUP BY colname";
$result = mysqli_query($connection, $query);
How can I pick up values from a postgreSQL database and place them in a combo box using php?
Here is a full example:
myPHP.php
<?php
$db = pg_connect('host=localhost dbname=contacts user=contacts password=firstphp');
$query = "SELECT * FROM friends";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
$list_data = null;
while($myrow = pg_fetch_assoc($result)) {
$list_data .= "<option value=\"".$myrow['id']."\">".$myrow['name']."</option>";
}
// assign it to some var, I dont know what king of template you are using
$t->assign('LIST_DATA',$list_data);
?>
myFile.tpl
<select name="one">
{LIST_DATA}
</select>