Change JSON value with PHP script - php

I have a JSON http://pastebin.com/index/LwAhi59r
And I have a PHP script in which I want to change my JSON value of each domain and template from each site. I would like to know what would be a good PHP code to change the values with an input and a selector in which you can choose which site you want to change it from. I got this php code I think it a good start but I don't know how to proceed.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Domain of Template veranderen met PHP script</h2>
<form action="process.php" id="form" method="post">
<select name="selector">
<option value="cms18.test.silverbee.nl">cms18.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">dmmd.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">opmaat.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">opmaatdebiteurenadvies.nl</option>
<option value="cms18.test.silverbee.nl">navbar.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">test18.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">huisartsplus.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">robertenrademaker.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">tilburg.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">cbk-groningen.test.silverbee.nl</option>
<option value="cms18.test.silverbee.nl">getbusyinc.test.silverbee.nl</option>
</select>
Value:<input type="text" name="value">
<input type="submit" value="Send">
</form>
<br>
<?php
$select = $_POST['selector'];
//neem de json bestand array in php
$lijst= file_get_contents('list.json');
/*echo $lijst;*/
//neem de lijst json en decodeerd het json string. als true maakt hij er een array van.
$decoderen= json_decode($lijst, true);
/*echo $decoderen;*/
echo "<ul>";
foreach ($decoderen as $key => $value)
{
echo "<li>".$key."<ul>";
foreach ($value as $key1 => $value1)
{
echo "<li>".$key1.": ".$value1."</li>";
}
echo "</ul>"."</li>";
}
echo "</ul>";
/*------------------------------------------------------------------------------------------------------*/
/*foreach ($decoderen as $key => $value) {
if((string)$key-> == $this->put('')) {
$value->name="--";(string)$value->description="--";
}*/ This is the last code i was trying to use i found this in another site someone had same problem but i couldnt come out with this. BTW i am new in PHP.
?>
</body>
</html>

Try something like this:
<?php
$list = file_get_contents('list.json');
$list = json_decode($list, true);
$selector = $_POST['selector'];
if (isset($selector))
{
// overwrite the selected domain of the list with the new value if they are not empty
if (isset($_POST['domain']))
{
$list[$selector]['domain'] = $_POST['domain'];
}
if (isset($_POST['template']))
{
$list[$selector]['template'] = $_POST['template'];
}
// store the new json
file_put_contents('list.json', json_encode($list));
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h2>Domain of Template veranderen met PHP script</h2>
<form action="process.php" id="form" method="post">
<select name="selector">
<?php foreach ($list AS $key => $value) : ?>
<option value="<?php echo $key; ?>"><?php echo $value['domain']; ?></option>
<?php endforeach; ?>
</select>
<input type="text" name="domain" placeholder="Domain">
<input type="text" name="template" placeholder="Template">
<input type="submit" value="Send">
</form>
</body>
</html>

Related

Getting radio input from selection in table

I'm trying to get my code to open a page that displays a picture of the model and displays information. the code uses a csv file that populates the table. I cant seem to figure out how to get my radio button to pass data based on selection. I'm trying to find the selected row and display the data on another page basically. any help would be appreciated. Thank you!
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>Choose One</h1>
<form action="index.php" method="post">
<label for="Type">Package Type</label>
<select name="Type">
<option value="">Choose...</option>
<option value="a">apple</option>
<option value="b">google</option>
<option value="c">oneplus</option>
<option value="d">Samsung</option>
</select>
<input type="Submit" >
</form>
<?php
echo "<html><body><table border = '1'>\n\n";
echo "<form action='stock.php' method='post' name='stock'>";
echo "<th>Item</th><th>Manufacturer</th><th>Model</th><th>Price</th>";
$myfile = fopen("phones.csv", "r");
$ind=0;
$ind++;
while (($dataarray = fgetcsv($myfile)) !== false)
{
echo "<tr><td>"."<input type = 'radio' name='manufacturer' id=manufacturer
value=$dataarray[$ind]>"."</td>";
foreach ($dataarray as $cell)
{
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
$ind = 0;
}
fclose($myfile);
echo "\n</table>
<input type='submit'>
</form></body></html>";
$ind = 0;
?>
</body>
</html>
Start of stock.php
<!DOCTYPE html>
<html lang="en">
<head>
<form action="index.php" method="get" name="stock">
</form>
<php?
if(isset($_get[manufacturer]))
{
$value=$_get[manufacturer];
}
?>
</head>
<body>
</body>
</html>
This is a snip of the csv file:

How to fix mysqli_fetch_array? I use php, mysqli and html

i have a problem with my php code, it gives me the error of : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\funcion2.php on line 69
I dont know how to solve it. i need some help pls, i need to finish the project.
<?php
//Conexion a la BBDD
include 'config.php';
session_start();
if($_REQUEST['cur']){$_SESSION['cur2']=$_REQUEST['cur'];}
$cur=$_SESSION['cur2'];
$str=$cur;
$cur=explode ('|', $str);
echo $cur[0];
if ($_REQUEST['alu']){$_SESSION['alu2']=$_REQUEST['alu'];}
$alu=$_SESSION['alu2'];
$str2=$alu;
$alu=explode ('|', $str2);
/*primera consulta*/
$query = 'select * from curso';
$res=mysqli_query($conexion, $query);
/*segunda consulta*/
$query2 = 'select * from alumnos where cod_curso=$cur[0]';
$res2=mysqli_query($conexion, $query2);
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
</head>
<body>
<form name="form1" method="post">
<div class="form-group col-md-3">
<label>Curso</label>
<select name="cur" onchange="this.form.submit() ;">
<option value="<?php echo $cur[1] ?>" ></option>
<?php
while ($row=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $row[0]."|".$row[1]?>"><?php echo htmlentities($row[1]); ?>
</option>
<?php } ?>
</select>
</div>
<div class="form-group col-md-6">
<label>Alumnos</label>
<select name="alu">
<option value="<?php echo $alu[0]?>"</option>
<?php
while ($value=mysqli_fetch_array($res2))
{
?>
<?php foreach ($alu as $key => $value){ ?>
<option value="<?php echo $value[0]."|".$value[1]?>"><?php echo htmlentities($value[1]);?>
</option>
<?php } ?>
<?php } ?>
</select>
</div>
<input type="submit" name="enviar" value="Enviar" hidden />
</form>
<?php
echo "Tu curso es: ".$cur[1]."<br/>";
echo "El alumno es: ".$alu[1]."<br/>";
?>
</body>
</html>
There is the problem
.The error os mysql
My database have these 2 tables:
Table alumnos and Table curso
My tables and the problem
I modified the parameters without # and the second mysqli_fetch_array.
Why are you sending the connection to the while loop for alumnos?
while ($value=mysqli_fetch_array($conexion, $cur))
It's expecting a DB array result, not the connection.
*******EDIT
Now that I've taken a look, you still have errors in this section.
<?php
while ($value=mysqli_fetch_array($res2))
{
?>
<?php
foreach ($alu as $key => $value){
?>
<option value="<?php echo $value[0]."|".$value[1]?>">
<?php echo htmlentities($value[1]);?>
</option>
<?php } ?>
<?php } ?>
Why do you have a for each loop inside a while loop? Unless $value has data that you need to loop inside each row, it is already being looped by the while loop and you would not need the for each.

How to display selected value in combo box in php?

I've written this code... but this fails to display text... not sure what's wrong with the code. I'm new to PHP and trying to create a page that gets customer details and puts it in SQL.
<!DOCTYPE html>
<html>
<body>
<?php
$initial="";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
echo "Customer Intial: $initial <br>";
?>
</body>
</html>
Try this:
$initial="";
if(isset($_POST['initial'])){
$initial=htmlentities($_POST['initial']);
}
You correctly sent the POST however you didn't put the value given with the POST into the $initial variable.
<!DOCTYPE html>
<html>
<body>
<?php
$initial = "";
?>
<form method="post"
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit"></form>
<br>
<br>
<?php
$initial = filter_input(INPUT_POST, "initial");//GET the input in post method
if ($initial == 1) {
$initial_value = "Mr.";
} elseif ($initial == 2) {
$initial_value = "Mrs.";
} elseif ($initial == 3) {
$initial_value = "Ms.";
} elseif ($initial == 4) {
$initial_value = "M/s";
} else {
$initial_value = "Select initial";
}
echo "Customer Intial: $initial_value <br>";
?>
</body>
</html>
Try,
<form method="post" action=<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>>
Intials: <select id="cmbInitial" name="initial" onchange="showUser(this.value)">
<option value="0">Select initial</option>
<option value="1">Mr.</option>
<option value="2">Mrs.</option>
<option value="3">Ms.</option>
<option value="4">M/s</option>
</select> <br>
<br>
<input type="submit" name="submit" value="Submit">
</form>
<br>
<br>
<?php
if(isset($_POST['initial'])){
$initial=$_POST['initial'];
} else {
$initial = "empty";
}
echo "Customer Intial : ".$initial;
?>
</body>
</html>

Using an array with SESSION or POST

I am trying to use the $_POST feature or $_SESSION feature of php to get an array based off form data from a previous pagepage. Whenever I try using $_SESSION I get the error:"The script tried to execute a method or access a property of an incomplete object." Here is an idea of what I'm trying to do. Any help would be great and thanks in advance!
<?php
// initialize a session
include 'hoteldetails.php';
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Search</h2></center>
<h3>Login <br/> Sign up</h3>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Please enter City, State or Zipcode:<input type="text" name="location">
<input type="date" name="startdate" value="startdate">
to <input type="date" name="enddate" value="enddate">
<br/>
Minimum price:<select name="minimumprice">
<option value="0">No Minimum</option>
<option value="49">$50</option>
<option value="99">$100</option>
<option value="199">$200</option>
</select>
Maximum price:<select name="maximumprice">
<option value="9999">No Maximum</option>
<option value="101">$100</option>
<option value="201">$200</option>
<option value="301">$300</option>
</select>
Beds:<select name="beds">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Bathrooms:<select name="baths">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/>
<input type="submit" name="Search" value="Search">
</form>
<?php
try{
$dbconn= pg_connect("host=localhost port=5432 dbname=roomReserves user=superUser password=F3Poriginal");
}
catch(PDOException $e){
echo $e->getMessage();
}
if(isset($_POST['Search'])){
if($_POST['startdate']==null || $_POST['enddate']==null || $_POST['location']==null){
echo "One or more fields are empty, please try again.";
}
else{
$query = "SELECT * FROM hotellist WHERE zip=75007";
$rs = pg_query($dbconn, $query) or die("Cannot execute query: $query\n");
// $hotelsdetail=array();
$_SESSION['hotelsdeets']=array();
$hotelpos=0;
while ($row = pg_fetch_row($rs)) {
$serverInfo= explode(" ",$row[2]);
$hotelsdb= pg_connect("host=$serverInfo[0] port=$serverInfo[1] dbname=$serverInfo[2] user=$serverInfo[3] password=$serverInfo[4]");
$queryresult = "SELECT * FROM hotelinfo where beds= CAST('".$_POST['beds']."' AS INT) AND baths= CAST(baths='".$_POST['baths']."' AS INT) AND price BETWEEN CAST('".$_POST['minimumprice']."' AS INT) AND CAST('".$_POST['maximumprice']."' AS INT)";
$hotelrs = pg_query($hotelsdb, $queryresult) or die("Cannot query hotel info: $queryresult\n");
$matchesquery=false;
while($hotelrow=pg_fetch_row($hotelrs) && $matchesquery==false){
$matchesquery=true;
$date = $_POST['startdate'];
$endDate= $_POST['enddate'];
$hotelroomrs=pg_fetch_row($hotelrs);
$hotelroom=$hotelroomrs[0];
$hotelprice=$hotelroomrs[1];
while ($date < $endDate && $matchesquery==true && $hotelroomrs[0]!=null) {
$isavail="SELECT * FROM hotelinfo where roomnumber= CAST('".$hotelroom."' AS INT) AND date= CAST('".$date."' AS date)";
$roomexists=pg_query($hotelsdb, $isavail);
$roomdets=pg_fetch_row($roomexists);
if($roomdets[0]==null){
$matchesquery=false;
}
$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
}
if($matchesquery==true && $hotelroomrs[0]!=null){
$_SESSION['hotelsdeets'][$hotelpos]=new hoteldetails();
$_SESSION['hotelsdeets'][$hotelpos]->sethotelinfo($row);
$_SESSION['hotelsdeets'][$hotelpos]->setprice($hotelprice);
$hotelpos++;
}
}
pg_close($hotelsdb);
}
header("Location: ./searchresults.php");
}
}
pg_close($dbconn);
?>
<?php
session_start();
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<center><h1>Room Reserves</h1>
<h2>Results</h2></center>
<h3>Login<br/>Sign up</h3>
</head>
<body>
<?PHP
//$searchinfo->$hotelsdetail[0]->sethotelinfo(1);
echo $_SESSION['hotelsdeets'][0]->gethotelinfo()[1];
?>
</body>
</html>
class hoteldetails {
private $hotelinfo;
private $price;
public function sethotelinfo($hotelinfo){
$this->hotelinfo=$hotelinfo;
}
public function setprice($price){
$this->price=$price;
}
public function gethotelinfo(){
return $this->hotelinfo;
}
public function get($var){
return $this->{$var};
}
}
Once again, THANKS!
As a starting point, you need to open and close the php tags:
<?PHP
session_start();
?>
<html>
<head>
</head>
<body>
<?PHP
$colors=array();
array_push($colors, "green");
array_push($colors, "white");
array_push($colors, "yellow");
array_push($colors, "black");
$_SESSION['color']=$colors;
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="submit" name="Search" value="Search">
</form>
<?php
if(isset($_POST['Search'])){
header("Location: page2.php");
}
?>

retrieve data and display in droplist (php/mysql)

can anyone tell me what is the error in this dam code i went crazy searching about the error but without any success
the error is that i do not get any data from the table why is that happening?? plz help me
index.php
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option id="<?php echo $viewallCountries['country_name']; ?> "></option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>
<html>
<head>
</head>
<body>
<form action="index.php" method="post">
<label for="searchByCountry">By Country</label>
<select name="searchByCountry" id="searchByCountry">
<option id="0">--select your country--</option>
<?php
require_once('connfor_lamelchameltest.php');
$getallCountries = mysql_query("SELECT country_name FROM country") or die("could not search for countries");
if(mysql_num_rows($getallCountries) > 0) {
while($viewallCountries = mysql_fetch_array($getallCountries)){
?>
<option value="<?php echo $viewallCountries['country_name']; ?> ">
<?php echo $viewallCountries['country_name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
</form>
</body>
</html>

Categories