Search box not returning data (PHP) - php

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
---------------------------------------------

Related

&& in Foreach loop (PHP)

Just stating learning PHP, I would like to know is there any way I can declare more than 1 $value in a foreach loop? I am trying to echo out my 8 different type of arrays which i declared ($line0 - $line8). Apologies if my codes are abit messy. I'm still new to PHP.
PHP Code
<?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 == 9){
$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);}
$row++;
}
?>
<table>
<tr>
<?php
foreach($line2 as $value1)&&(line3 as $value2){
echo "<td><b>Product ID: $value1</b>"
echo "<td><b>Selection ID: $value2</b>
</td>";
echo '</tr>';
}
?>
</table>
listings.txt
Cedric
93482812
cedric#hotmail.com
Guitar
---------------------------------------------
Wendy
98238432
wendy#hotmail.com
Guitar
---------------------------------------------
No you can't do that, but there's something else you can use. All your arrays have the name keys, and you can get the key with foreach like this:
foreach ($line1 as $key => $value1) {
$value2 = $line2[$key];
echo "<tr>";
echo "<td><b>Product ID: $value1</b></td>";
echo "<td><b>Selection ID: $value2</b></td>";
echo '</tr>';
}
That is getting pretty close to what you want.

PHP empty variable inside condition

I have a condition in which it is using a variable to pull either a number through 0-17, the string "MAKEUP", or the variable will be empty. I would like it to output the text "WIN" if the variable is greater than the number 8 and "LOSS" if the variable is less than the number 9. I would also like it to out "MAKEUP" if the variable consist of the string MAKEUP, and to display nothing if the variable is empty. Seems pretty simple to me, but I'm having issues particularly with the empty part. Can anyone let me know what I am doing wrong here? Code below
<?php
$t1w8 = '';
$result = $t1w8;
if ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result < 9 && !empty($result)) {
echo 'LOSS';
} elseif ($result == 'MAKEUP') {
echo '-';
} else {
echo 'yooo';
}
?>
Make some changes in your conditions like this
<?php
//$result = "MAKEUP";
$result = 0;
if ($result === 'MAKEUP') {
echo '-';
}else if (is_numeric($result) && $result < 9 ) {
echo 'LOSS';
}else if (is_numeric($result) && $result >= 9 ) {
echo 'WON';
} else{
echo 'yooo';
}
?>
Live demo : https://eval.in/897120
try with this code
<?php
//$result = "MAKEUP";
$result = "";
//$result = "9";
//$result = "-";
if ($result == 'MAKEUP' && !empty($result) ) {
echo '-';
} elseif ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result <= 8 && !empty($result)) {
echo 'LOSS';
} else {
echo 'yooo';
}
?>
for demo :demo code here
You have explained that your number range is from 0-17.
You have also explained that you could get the word MAKEUP.
Based upon those constraints we could use something like this
$output = "";
// Do we have something?
if(strlen($result) > 0) {
if (strtolower($result) == "makeup") {
$output = "MAKEUP";
}
// assumes a single digit string
else if ($result < 9) {
$output = "LOSS";
} else if ($result <= 17) {
$output = "WIN";
}
}
echo $output;

How to Compare related strings?

so i have a problem with my program where i search a name in an array called ArrName then display handsome or beautiful if he/she exist in the array but if the name is not in the array then the program will display the related strings if the string that i searched does not exist inside the array. i have a hard time solving this please help.Here is a my example.
SAMPLE OUTPUT #1:
Names: Bob, John, Charlie, Bravo, Alpha, Raymond, Kenneth, Rose, Rosel, James
Searching for: bob.
Result: Bob exist, Bob is handsome
SAMPLE OUTPUT#2:
Names: Bob, John, Charlie, Bravo, Alpha, Raymond, Kenneth, Rose, Rosel, James
Searching for: roger.
Result: Roger not found. did you mean:
Rose?
Rosel?
here is my code so far:
<html>
<body>
<?php
$arrNumbers = array('jonel','jon','john','rosel','rosil','rose','ramon','ramin','ramoon','kenneth','keneth','kennet','joy','juy');
$arrboys = array('jonel','jon','john','ramon','ramin','ramoon','kenneth','keneth','kennet');
$arrgirls = array('rosel','rosil','rose','joy','juy');
$strsearch = 'rosel';
$count = 0;
$badd = 0;
$gadd = 0;
foreach($arrNumbers as $value)// checks if name exists
{
if($value == $strsearch)
{
$count = $count + 1;
}
else
{
$count = $count + 0;
}
}
if($count > 0)// if name exists, checks if the name is from a boy or a girl
{
//boys
foreach($arrboys as $value)// checks if the name is from the boys
{
if($value == $strsearch)
{
$badd = $badd + 1;
}
else
{
$badd = $badd + 0;
}
}
if($badd > 0)
{
echo $strsearch.' is handsome';
}
// girls
foreach($arrgirls as $value)// checks if the name is from the girls
{
if($value == $strsearch)
{
$gadd = $gadd + 1;
}
else
{
$gadd = $gadd + 0;
}
}
if($gadd > 0)
{
echo $strsearch.' is beautiful';
}
}
else// if the name does not exists
{
// this is the part where i dont know what to do.
// this part suppose to display all the names related to the name that is
// being searched.
echo 'did you mean';
}
?>
</body>
Why are you checking occurrence in $arrNumbers array as $arrboys and $arrgirls are substring of $arrNumbers.
Use below code
<?php
$arrNumbers = array('jonel','jon','john','rosel','rosil','rose','ramon','ramin','ramoon','kenneth','keneth','kennet','joy','juy');
$arrboys = array('jonel','jon','john','ramon','ramin','ramoon','kenneth','keneth','kennet');
$arrgirls = array('rosel','rosil','rose','joy','juy');
$strsearch = 'rosel';
$flag = '';
for($i=0;$i<sizeof($arrboys);$i++)
{
//echo $arrboys[$i].' ';
if($strsearch == $arrboys[$i]);
$flag = 'boy';
}
for($i=0;$i<sizeof($arrgirls);$i++)
{
//echo $arrboys[$i].' ';
if($strsearch == $arrgirls[$i]);
$flag = 'girl';
}
if($flag == 'boy')
{
echo $strsearch.' exist, '. $strsearch.' is handsome';
}
if($flag == 'girl')
{
echo $strsearch.' exist, '. $strsearch.' is beautiful';
}
else
{
echo $strsearch.' not found. did you mean:';
}
?>

PHP - refreshing page and showing different rows in a table

<?php
//if pageNum isset figure out where to start(7 rows per page * page num +1
$pageNum = isset($_GET['pageNum']) ? (int)$_GET['pageNum'] : 0;
$startRow = $pageNum == 0 ? 0 : ($pageNum * 7 + 1);
$endRow = $startRow + 7;
$count = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if($count >= $startRow)
echo ' aantal vervangingen: 30';
$row = 1;
if (($handle = fopen("vervangingen.csv", "r")) !== FALSE) {
echo '<table border="1">';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
if($count == $endRow)
{
//wait 3 seconds then use javascript to redirect.
sleep(3);
echo '<script>window.loaction.href="theurl?pageNum='.($pageNum +1).'"</script>';
}
}
?>
I made a table in php which gets the data from a .csv file. My question is, how do i let php show the first 7 rows, then it should refresh the page and show the following 7 rows, there are 30 in total. Once it has shown all the rows, it should still refresh the page and start all over again.
How do I do this? I know how to refresh a php page, but showing 7 rows per refresh is quite hard. Any help?
Greetings
The following (tested) uses PHP sessions and JS setTimeout.
Interesting, but I wonder if I will ever use this effect.
(I was unable to save the $handle as a session variable.)
<?php // z1.php is this file, z1.csv is the data file
session_start();
echo <<<EOD
<body onload="setTimeout('f1();',3000);">
<script type="text/javascript">
function f1() { window.location.replace("z1.php"); }
</script>
EOD;
if (isset($_SESSION['sessrow1st'])) { $row1st = $_SESSION['sessrow1st']; }
else { $row1st = 1; }
$handle = fopen("z1.csv", "r");
if ($handle === false) { exit("open error"); };
echo "<table border='1'>\n";
$rownum = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$rownum += 1;
if ($rownum < $row1st) continue;
if ($rownum > $row1st+6) break;
$numcols = count($data);
echo '<tr>';
for ($c=0; $c < $numcols; $c++) {
if(empty($data[$c])) { $value = " "; }
else { $value = $data[$c]; }
echo '<td>'.$value.'</td>'; }
echo "</tr>\n"; }
echo '</table></body>';
if (feof($handle)) { $rownum = 1; }
fclose($handle);
$_SESSION['sessrow1st'] = $rownum;
?>
You can use Javascript's setTimeout and a get variable to accomplish this.
//if pageNum isset figure out where to start(7 rows per page * page num +1
$pageNum = isset($_GET['pageNum']) ? (int)$_GET['pageNum'] : 0;
$startRow = $pageNum == 0 ? 0 : ($pageNum * 7 + 1);
$endRow = $startRow + 7;
$count = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if($count >= $startRow)
{
$num = count($data);
if ($row == 1) {
echo '<tr>';
}else{
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
//echo $data[$c] . "<br />\n";
if(empty($data[$c])) {
$value = " ";
}else{
$value = $data[$c];
}
if ($row == 1) {
echo '<th>'.$value.'</th>';
}else{
echo '<td>'.$value.'</td>';
}
}
if ($row == 1) {
echo '</tr></thead><tbody>';
}else{
echo '</tr>';
}
$row++;
}
if($count == $endRow)
{
//make it dynamic...
$theUrl = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?pageNum=".($pageNum+1);
//wait 3 seconds then use javascript to redirect.
echo '<script>setTimeout(function(){
window.location.href="'.$theUrl.'"}, 3000)
</script>';
}
++$count;
}
First step is figure out which page you're on, then display what you want, then use javascript redirect, as php redirect will fail after headers are sent. Also the setTimeout function is what controls the wait

Unxpected result when iterating nested arrays for merging content in PHP

I have 2 nested arrays over which I intent to iterate over one and insert into it a portion of a match in the other based on a key->value.
The idea is to iterate for each element of arrayA and nested iterate for each key->value of arrayB. When arrayA element equals to arrayB key->value i want to insert into arrayB a key->value of arrayA.
The problem I am having is that for some reason in the loop of arrayB it should iterate 78 times but is doing it only 2.
I know there is something messed up with the iterations but can't pin point.
Here is a sample of arrayA, arrayB and my code.
arrayA
arrayB
Here is my code
foreach($premiumContent as $prem_key => $targets)
{
$totCat = 0;
foreach($result as $category) //(result = 2 shop, ff) (category shop.designers, ff.restaurant)
{
$totCat = $totCat + 1;
$totFeatures = 0;
foreach($category as $features) //features = 1 designers, restaurants
{
$totFeatures = $totFeatures + 1;
//*********HERE IS THE PROBLEM IS NOT ITERATING BY THE LIST OF ALL FEATURES
$totFeature = 0;
foreach ($features as $feature) //stores
{
$totFeature = $totFeature + 1;
$properties = $feature[0]->properties;
if ($feature[0]->id == $prem_key)
{
if(count($targets[media]) > 0)
{
$properties->media = $targets[media];
}
else
{
$properties->media = '';
}
if(count($targets[offer]) > 0)
{
$properties->offer = $targets[offer];
}
else
{
$properties->offer = '';
}
if(count($targets[bi]) > 0)
{
$properties->bi = $targets[bi];
}
else
{
$properties->bi = '';
}
if(count($targets[info]) > 0)
{
$properties->info = $targets[info];
}
else
{
$properties->info = '';
}
}
}
}
}
}
return $result;
Could someone explain me what am I doing wrong? I am sure there is a better approach to this.
Here is the fix.
foreach($result as $category) //(result = 2 shop, ff) (category shop.designers, ff.restaurant)
{
$name = $category->dispName;
//echo $name;
foreach($category->list as $features) //features = 1 designers, restaurants
{
foreach ($features as $featureKey => $featureVal) //stores
{
$properties = $featureVal->properties;
if ($featureVal->id == $prem_key)
{
if(count($targets[media]) > 0)
{
$properties->media = $targets[media];
}
else
{
$properties->media = '';
}
if(count($targets[offer]) > 0)
{
$properties->offer = $targets[offer];
}
else
{
$properties->offer = '';
}
if(count($targets[bi]) > 0)
{
$properties->bi = $targets[bi];
}
else
{
$properties->bi = '';
}
if(count($targets[info]) > 0)
{
$properties->info = $targets[info];
}
else
{
$properties->info = '';
}
}
}
}
}
}

Categories