PHP parse MYSQL after comma and output to html table - php

Thank you all for you help! I received some phenomenal help about 17 days ago which saved my *!. I am still learning PHP and have run across a new problem. I have a MYSQL database field called "Tracking" this is a long text field in which we want to input tracking numbers (for shipping) separated by commas . We ship some times 20-50 individual boxes in the same order.
The following script outputs to an HTML table the number of boxes being shipped (EX Box 1 of 12, Box 2 of 12...etc) and then the value of the tracking number field. I need to modify it so it parses at the "comma" in the Tracking field and includes the tracking number for each box. So Box 1: Tracking Number "take first tracking number and end at comma", So Box 2: Tracking Number "take second tracking number and end at comma"....etc...does this make sense?
Please provide whatever help possible, would greatly be appreciated!
Script:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('***','***','***','***');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db116223_wmi");
$sql="SELECT * FROM tbl_purchase_order WHERE purchase_order_id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
for ($i=0; $i<$row['product_quantity']; $i++) {
$my_val=1;
$x += 1;
echo "<table>";
echo "<tr>";
echo "<td width=400><br>Box $x of ". $row['product_quantity'] ." Tracking Number:". $row['Tracking'] ."</td>";
echo "</tr>";
echo "</table>";
}
}
mysqli_close($con);
?>
JB

As jjonesdesign mentioned, you need to use explode to break the tracking number field up at each comma. explode creates an array of values, each split out of the Tracking field. Look below for the solution:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('***','***','***','***');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db116223_wmi");
$sql="SELECT * FROM tbl_purchase_order WHERE purchase_order_id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$myTracking = explode(',',$row['Tracking']); // get an array of tracking numbers. Array element starts with 0
for ($i=0; $i<$row['product_quantity']; $i++) {
$my_val=1;
$x += 1;
echo "<table>";
echo "<tr>";
echo "<td width=400><br>Box $x of ". $row['product_quantity'] ." Tracking Number:". $myTracking[$i] ."</td>";
echo "</tr>";
echo "</table>";
}
}
mysqli_close($con);
?>

Related

Compare num records returned to a set integer and display message if true

How to compare number of rows returned to a number and display message
I have a form with a drop down where people can select either a serial number or the name of a product and a textbox where they can type their search. I have the minimum length set to 1 because the serial numbers are minimum 1 digit and range up to 5 digits. Also, so that if they only know the first letter of the name it lists all the records starting with that letter. If they type the name of the product and only know the first letter of it, I'd like to display a message if they only type one letter in the name that says something like "Please try typing more than one letter." I can succesfully display this message, but if the number of records return is less than 5 it still displays.
I wrote code on the processing page for the form, so that it counts the number of rows returned and checks to see if it is greater than or equal to 5. I wanted to display this message if the search returns more than 5 records.
My question: How do I compare the number of records returned to the set number of 5 so that my message to go back and try a longer search (for the name of product) displays only when there are 5 or more records returned? It seems to display the message fine, but when <5 records are returned, it still displays the message.
my code processing page PHP code:
include "includeData.php";
$conn = mysqli_connect("localhost", $mysqlUname,$mysqlPW, $mysqlDB);
if (!$conn){
die('Could not connect: ' . mysqli_connect_error());
}
if (!empty($_GET['searchText'])){
$query = $_GET['searchText'];
$min_length = 1;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysqli_real_escape_string($conn,$query);
$sql = "SELECT * FROM machine WHERE `m_ipdb` LIKE '%".$query."%' OR `m_name` LIKE '%". $query."%'";
$qResults = mysqli_query($conn,$sql);
if (!$qResults){
echo "There was a problem!<br />";
die(mysqli_error($conn));
//added for best practice and consistency
}
echo "<table border='1'>";
while($row = mysqli_fetch_array($qResults))
{
echo "<tr>";
echo "<td>" . $row['m_ipdb'] . "</td>";
echo "<td><a href=\"details.php?m_ipdb=" . $row['m_ipdb'] . "&m_name=" . $row['m_name'] . "\">";
echo $row['m_name'];
echo "</a></td>";
echo "</tr>";
}
echo "</table>";
/*echo "<p>Number of rows returned: " . mysqli_num_rows($qResults) . "<br />";*/
$result = mysqli_query($conn,"SELECT COUNT(*) AS number FROM machine");
$num = mysqli_fetch_array($result);
$count = $num["number"];
if($count >= 5){
echo "<a href='search.php'>Return to Search Page</a><br />" . "And try searching for more than one letter in the name!";
}
else{
echo " ";
}
mysqli_close($conn);
}
}

how to i calculate the total sum

As the title stated;
How do i calculate TOTAL when user checked multiple checkboxs on the selected hotel?
and
How do i make it so that whenever a TOTAL is added and the current TOTAL (which is in mysql) exceed 500k, an alert will pop saying BUDGET EXCEEDED
Thanks you!
CODE:
<?php
require ("config.php");
$link = mysqli_connect($h,$u,$p,$db)
OR die(mysqli_error());
$query = "select BIL, HOTEL, TOTAL, address from HOTELS"
OR DIE(mysqli_error());
$result = mysqli_query($link,$query);
echo "<table border=2>";
echo "<tr><th><b>HOTEL</th><th><b>TOTAL(RM)</th><th>ADDRESS</th></tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>$row[HOTEL]</td>";
echo "<td>RM $row[TOTAL]</td>";
echo "<td>$row[address]</td>";
echo "<td><a href='delete.php?del=$row[BIL] '>Delete</a></td>";
echo "<td><input name='checkbox[]' type='checkbox' value='{$row['HOTEL']}'></td></tr>" ;
}
?>
Use JavaScript or JQuery (JS library).
Whenever a checkbox selected/deselected, retrieve the value from corresponding 'total' cell
$selected_val = $(this).parent.(".total").text //sample in jquery,
and remove the 'RM' word. then +/- the value in total.

Select one value from database

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

Parsing one field to fill another field in MySQL

I am new to PHP/MySQL and am working my way through the basics.
I have a MySQL database scwdb (that I moved from Access 2000 which my Windows 7 won't work with) with a table tblsplintersbowlinventory which has 2 fields:
fields and data:
txtProductBowlCode
data examples: OakSc07-001, MapleTi07-030, MapleTi07-034, BlackLimba07-002, AshSc07-017
txtProductPrimarySpecies
data examples: Oak, Maple, Maple, BlackLimba, Ash
In other words, I want to record just the species in the txtProductPrimarySpecies field.
I tried the following PHP script:
<?php
$con = mysql_connect("localhost","xxxxxxx","zzzzzzz");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("scwdb", $con);
$species = 'Maple';
mysql_query("UPDATE tblsplintersbowlinventory WHERE txtProductBowlCode LIKE $species SET txtProductPrimarySpecies=$species%");
echo "done";
mysql_close($con);
?>
It seems to run, does not show an error and prints "done", but when I check the database I don't see any changes.
What am I missing?
This db has over 600 records, and I added this new txtProductPrimarySpecies field to make my searches easier while leaving the full code which has specific info on the bowl. There are several species that I need to do this to, so I plan on using a loop to run through a list of species.
How would I code that loop to read a list of species?
OK, I found the way to make this work!
<?php
$con = mysql_connect("localhost","xxxxxx","zzzzzzz");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("scwdb", $con);
$species = 'Maple';
$result = mysql_query("UPDATE tblsplintersbowlinventory SET txtProductPrimarySpecies = '$species' WHERE txtProductBowlCode LIKE '$species%'");
$result = mysql_query("SELECT * FROM tblsplintersbowlinventory WHERE txtProductBowlCode LIKE '$species%'");
echo "<table border='1'>
<tr>
<th>Index</th>
<th>Bowl Code</th>
<th>Species</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['intProductID'] . "</td>";
echo "<td>" . $row['txtProductBowlCode'] . "</td>";
echo "<td>" . $row['txtProductPrimarySpecies'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "done";
mysql_close($con);
?>
This worked, and I manually changed the $species value and ran it for each of the species of wood in the database...since this was a one time shot it made more sense not to use a list and loop through it - I was bound to miss one or two species anyway.
Shouldn't the set without % come before where with %. Also I think your parameter should be wrapped with a quote as it is string type.
mysql_query("UPDATE tblsplintersbowlinventory
SET txtProductPrimarySpecies='$species'
WHERE txtProductBowlCode LIKE 'CONCAT($species, '%')'");

Recalling random values from mysql table

I'm fairly new to php and mysql and I'm on the home stretch of finishing my page but I've been banging my head on the keyboard all day trying to figure out how to fix this problem. I've set up a php script to run as a cron even every 24 hours. The script assigns a random number between 10 and 30 to each field in my table. That works fine and every time I load the script the values change.
The problem I'm having is when I try to use those values. The result keeps printing as the word Array instead of the number in the table. So I'll give you some snippets of the code I'm running here. This is the cron event.
?php
$con = mysql_connect("localhost","username","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$random= rand(10, 30);
mysql_query("UPDATE winners SET pool= '$random'");
mysql_close($con);
And here is the script to call up the values.
php?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row % 2 )
{
echo "<h4>Result 1</h4>";
echo "$row";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
I've tried every possible variation I can think of to this code but all I can get is it to echo either Array or Resource #9. Any insight into this would be greatly appreciated.
You do not want to echo the content of $row, which contains all data returned for the current line you've fetched from the database when calling mysql_fetch_array().
Instead, you want to access the content of $row's pool item :
echo $row['pool'];
You should probably take a closer look at the manual page for mysql_fetch_array(), and the examples it contains.
Note that you'll probably also want to modify the condition in the following line :
if ( $row % 2 )
You probably don't want the test to be done on $row, but on an item it contains.
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "$row['pool']";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
}
Hope this helps.

Categories