I have a simple search box which allows me to search for a "Product ID". it will then read through my txt.file to find it. After reading, it will return the full information of the product based on the Product ID searched. As of right now, I'm having trouble returning the information of the product in the next page. I guess its because I don't really know how to compare the product IDs which are in a combination of strings, int and dashes and that's why it returns an empty result.
P.S: the first line of every element in my txt is the Product ID.
My Search Box
<html>
<body>
<div class="sn">
<form method="GET" action="fullInfo.php">
<p style="text-align: center;">Search for your musical instrument here!</p><br>
<input name ="sn" type="text" placeholder="Enter Product ID"><br>
<input type="submit" value="Search" class="submit" >
</form>
</div>
</body>
</html>
My Search Result (PHP)
<?php
$handle = #fopen('listings.txt', "r");
$row = 0;
$count = 0;
$line0 = [];
$line1 = [];
$line2 = [];
$line3 = [];
$line4 = [];
$line5 = [];
$line6 = [];
$line7 = [];
$line8 = [];
if ($handle) {
while (!feof($handle)) {
$store = fgets($handle, 4096);
if ($row == 10){
$row = 0;
$count++;
}
if ($row == 0)
{
$line0[] = strval($store);
}
else if($row == 1) {
$line1[] = strval($store);
}
else if($row == 2) {
$line2[] = strval($store);
}
else if($row == 3) {
$line3[] = strval($store);
}
else if($row == 4) {
$line4[] = strval($store);
}
else if($row == 5) {
$line5[] = strval($store);
}
else if($row == 6) {
$line6[] = strval($store);
}
else if($row == 7) {
$line7[] = strval($store);
}
else if($row == 8) {
$line8[] = strval($store);
}
$row++;
}
$sn = 0;
if (isset($_GET['sn'])) {
$sn=$_GET['sn'];
}
$item = count($line0);
for ($c=0; $c<$item; $c++)
{
if((intval(strnatcmp ($line0[$c], $sn)) == 0)) {
echo $line0[$c],"<br>";
echo $line1[$c],"<br>";
echo $line2[$c],"<br>";
echo $line3[$c],"<br>";
echo $line4[$c],"<br>";
echo $line5[$c],"<br>";
echo $line6[$c],"<br>";
echo $line7[$c],"<br>";
echo $line8[$c],"<br>";
break;
}
}
?>
<?php
fclose($handle);
}
?>
My listings.txt
abc-1234-99
Cedric
93482812
cedric#hotmail.com
Guitar
2000
Yamaha
null
New
---------------------------------------------
def-4332-99
Wendy
98238432
wendy#hotmail.com
Guitar
2010
Yamaha
Scratches on the side
Used
---------------------------------------------
fgh-4567-99
Wendy
98238432
wendy#hotmail.com
Guitar
2010
Yamaha
Scratches on the side
Used
---------------------------------------------
I'm trying to fetch arrays from two queries. The result only gets the first index.
while(($row2 = mysql_fetch_array($query1)) && ($index = mysql_fetch_array($query2)))
{
$leaveID = $row2['leaveID'];
$personID = $index['personID'];
$Person_Type = $index['Person_Type'];
if ($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
Assuming both queries are OK I offer two ways to loop through them depending on your needs. Hopefully one of them will help.
<?php
$arrayRowA = array();
$arrayRowB = array();
while($row = mysql_fetch_array($query1)){$arrayRowA[] = $row;}
while($row = mysql_fetch_array($query2)){$arrayRowB[] = $row;}
// Loop through two arrays in a square way (every combination of both arrays)
foreach($arrayRowA as $keyA => $objectA){
foreach($arrayRowB as $keyB => $objectB){
$leaveID = $objectA['leaveID'];
$personID = $objectB['personID'];
$Person_Type = $objectB['Person_Type'];
if($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
}
// Loop through two arrays in a linear way (one to one)
foreach($arrayRowA as $keyA => $objectA){
if(isset($arrayRowB[$keyA])){
$objectB = $arrayRowB[$keyA];
$leaveID = $objectA['leaveID'];
$personID = $objectB['personID'];
$Person_Type = $objectB['Person_Type'];
if($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
echo "</tr>";
}
}
?>
$query = mysql_query("select p*, l* from person as p left join leave as l on p.personID = l.leaveID");
while(($row = mysql_fetch_array($query)))
{
$Person_Type = $row['Person_Type'];
if ($Person_Type == 'Regular')
{
$Sick_Remaining_Days = 10 - $Sick_Total_Days;
}else{
$Sick_Remaining_Days = 5 - $Sick_Total_Days;
}
echo "<tr>";
echo "<td>$Sick_Remaining_Days</td>";
echo "</tr>";
}
You should get array before while:
array f,s
while(first)
{
f = ...;
}
while(second)
{
s = ...;
}
And now use both array.
Or try use new http://php.net/manual/en/mysqli-result.fetch-array.php. Probably you losing connection to first query after use second query.
while($row = mysqli_fetch_assoc($result))
{
$row2 = mysqli_fetch_assoc($result2);
echo $row['id'];
echo $row2['id'];
/* your code */
} `
Use this... It is what do you search !
while($row2 = mysql_fetch_array($query1))
{
$index = mysql_fetch_array($query2);
echo $row['id'];
echo $index['id'];
/* add code */
}
hey guys im trying to get the even indexes of a string from the db then save them in a variable then echo. but my codes seems doesnt work. please help. here it is
require_once('DBconnect.php');
$school_id = '1';
$section_id = '39';
$select_pk = "SELECT * FROM section
WHERE school_id = '$school_id'
AND section_id = '$section_id'";
$query = mysql_query($select_pk) or die (mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$public_key = $row['public_key'];
}
if ($public_key) {
$leng_public_key = strlen($public_key);
$priv_key_extract = "";
$array_pki = array();
for ($i=0; $i <=$leng_public_key-1 ; $i++) {
array_push($array_pki,$public_key[$i]);
}
foreach ($array_pki as $key => $value) {
if($key % 2 == 0) {
$priv_key_extract += $public_key[$key];
} else {
$priv_key_extract ="haiiizzz";
}
}
}
echo $priv_key_extract;
as you can see im trying to use modulo 2 to see if the index is even.
I have updated your code as below, it will work now :
<?php
$public_key = 'A0L8V1I5N9';
if ($public_key) {
$leng_public_key = strlen($public_key);
$priv_key_extract = "";
$array_pki = array();
for ($i=0; $i <=$leng_public_key-1 ; $i++) {
array_push($array_pki,$public_key[$i]);
}
foreach ($array_pki as $key => $value) {
//Changed condition below $key % 2 ==0 => replaced with $key % 2 == 1
if($key % 2 == 1) {
// Changed concatenation operator , += replaced with .=
$priv_key_extract .= $public_key[$key];
} /*else {
//Commented this as it is getting overwritten
$priv_key_extract ="haiiizzz";
}*/
}
}
echo $priv_key_extract;
?>
Try this function
function extractKey($key) {
if (empty($key) || !is_string($key)) return '';
$pkey = '';
for ($i=0;$i<strlen($key);$i++) {
if ($i % 2 == 0) {
$pkey .= $key[$i];
}
}
return $pkey;
}
echo extractKey('12345678'); # => 1357
I have 568 rows in my csv file and my for loop looks like this
$csv = array();
$file = fopen('names.csv', 'r');
while (($result = fgetcsv($file)) !== false)
{
$csv[] = $result;
}
fclose($file);
for ($row = 0; $row < 568; $row++)
{
echo "Serial no:<br/>";
echo "Name:".$csv[$row][1]."";
}
I want the output like this...
Serial no: 1
Name: Blah blah 1
Serial no: 2
Name: Blah blah 2
............
............
Serial no: 10
Name: Blah blah 10
For each 10 rows i want serial 1 to 10.. Once it finished 10 rows i want a horizontal line..
I mean i want to print
echo "<hr>";
For every 10 rows..
Can anyone help me?
Thanks
This should work :
for ($row = 0; $row < 568; $row++) {
echo "Serial no:<br/>";
echo "Name:".$csv[$row][1];
echo (($row+1)%10 == 0) ? '<hr>' : '<br />';
}
Explanation :
You don't need that ."" at the end of your "Name" line.
$row + 1 : instead of $row to avoid printing an <hr> after the first element (pos 0)
echo (condition) ? res1 : res 2; is like if (condition) echo res1; else echo res2;
But the real good way to do this would be :
$file = fopen('names.csv', 'r');
$i = 1;
while (($result = fgetcsv($file)) !== false) {
echo "Serial no:" .$i. "<br/>";
echo "Name:".$result[1];
echo ($i == 1) ? '<hr>' : '<br />';
$i = ($i%10)+1;
}
fclose($file);
$csv = array();
$file = fopen('names.csv', 'r');
while (($result = fgetcsv($file)) !== false)
{
$csv[] = $result;
}
fclose($file);
for ($row = 0; $row < 568; $row++)
{
echo "Serial no:<br/>";
echo "Name:".$csv[$row][1]."";
if ((int) $row % 10 === 0)
{
echo '<hr>';
}
}
Should do the trick :)
This code works perfectly except that it doesn't print the last 2 rows of my csv file:
This is the file:
603629,0,ATLV0008,"Vendor1",1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP
603627,2,ATLV0008,"Vendor1",1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP
600023,0,FLD4V0003,"Vendor2",1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP
600024,0,FLD4V0003,"Vendor2",1800.00,06/15/2010,1800.00,6/14/10 Request,EFT-JP
603631,0,ATLV5066,"Vendor2",1000.00,11/09/2010,1000.00,11/4/10 Check Request,PM2
603647,0,ATLV5027,"DVendor3",2799.80,11/15/2010,2799.80,10/1-10/31/10 Bishop,PM2
603642,5,ATLV5027,"Vendor3",482.40,11/15/2010,482.40,10/1-10/18/10 Allen,PM2
603653,0,ATLV0403,"Vendor4",931.21,11/17/2010,931.21,9/1-9/30/10,EFT-JP
603661,0,ATLV0105,"Vendor5",26.75,11/19/2010,26.75,093139,PM2
603660,1,ATLV0105,"Vendor5",5.35,11/19/2010,5.35,093472,PM2
Here is the code: (It needs to display the sum of 2 rows with the same vendor before the actual rows)
if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
while (($row_array = fgetcsv($handle, 1000, ","))) {
foreach ($row_array as $key => $val) {
$row_array[$key] = trim(str_replace('"', '', $val));
ob_flush();
}
$complete[] = $row_array;
ob_flush();
}
}
flush();
$prevVendor = '';
$sum = 0;
$venData = '';
if (isset($complete)) {
foreach ($complete as $key => $val) {
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != NULL){
print "<br/>";
print $sum . "<br/>";
print $venData;
$sum = 0; $venData = '';
}
}
foreach ($val as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . ' ';
$prevVendor = $val[3];
}
}
if ($currVendor == $prevVendor){
$sum += $complete[$key][6];
}
}
}
I don't really understand your problem but if you want to get (and save it wherever you want) the sum of each vendors, you should do something like this :
$prevVendor = null;
$venData = '';
$sums = array();
if (isset($complete) && is_array($complete)) {
$lim = count($complete);
for ($key=0;$key<$lim;++$key) { // no need foreach for simple array
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != null){
print "<br/>";
print $venData;
}
$venData = '';
$prevVendor = $currVendor;
$sums[$currVendor] = 0; // set the counter to 0
}
foreach ($complete[$key] as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . ' '; // is useful for you I guess
}
}
$sums[$currVendor] += $complete[$key][6]; // add amounts
}
}
if ($prevVendor != null){ // you need to do this to display the last record
print "<br/>";
print $venData;
}
var_export($sums); // check results
This can be some syntax errors ...