I have built an OpenCart module and tested it on a few shops and it works well. One client just reported to me a problem and after inspecting the html I saw this:
My code in the .tpl template file:
<table class="table table-bordered table-hover" >
<tbody>
<?php foreach($row['strings'] as $row_string){
$key = $row_string['key'];
$id = $row['simpleFilePathEscaped'].$key; ?>
<tr>
<td class="text-center">
<label><?php echo $key; ?> </label>
</td>
<td class="text-center">
<div>
<input id="text_value_<?php echo $id; ?>" type="text" value="<?php echo $row_string['value']; ?>" placeholder="<?php echo $key; ?>" class="form-control" />
</div>
</td>
<td class="text-center" >
<button id="save_icon_<?php echo $id; ?>" onclick="addLiteralToFile('<?php echo $row['secondary_file_path_escaped'] ?>', '<?php echo $key; ?>', '<?php echo $id;?>');" class="btn btn-primary"><i id="save_inner_icon_<?php echo $id; ?>" class="fa fa-save"></i></button>
<div id="loading_icon_<?php echo $id; ?>" <div class="loader"></div> </div>
</td>
</tr>
<?php } ?>
</tbody>
</table>
The result for an example row in this client's shop is this:
<tr>
<td class="text-center" >
<label>direction</label>
</td>
<td class="text-center">
<div>
<input id="text_value_" type="text" value="ltr" placeholder="direction" class="form-control">
</div>
</td>
<td class="text-center">
<button id="save_icon_" onclick="addLiteralToFile('----home----admin----domains----itrend.si----public_html----test----admin----language----english----en-gb.php', 'direction', '');" class="btn btn-primary"><i id="save_inner_icon_" class="fa fa-save"></i></button>
<div id="loading_icon_" <div="" class="loader"></div>
</td>
</tr>
If you notice you will see that all instances of <?php echo $id; ?> give an empty string. so id="save_icon_<?php echo $id; ?>" becomes id="save_icon_"
That is extremely strange because $id is a concat of $row['simpleFilePathEscaped'] and $key. Even if $row['simpleFilePathEscaped'] is empty I know for sure that $key has a value...Because it is echoed and in this example it is "direction" (in the label tag)
Need some help finding out why this is happening...
$row = { "strings" => array of { "key"=> string, "value"=> string },
"simpleFilePathEscaped" => string,
"secondary_file_path_escaped" => string,
"primary_file_path"=> string
}
Sorry if i misunderstood the way your $row is build.
if you make an example like
$row['strings'] = ['key' => 10];
$row['strings'] = ['key' => 110];
foreach($row['strings'] as $row_string)
$key = $row_string['key'];
print_r($key); //will give you an empty value
but print_r($row); // will give Array ( [strings] => Array ( ['key'] => 110 ) )`
you need to change your foreach.
foreach($row['strings'] as $row_key => $row_string)
$key = $row_key;
then it will give the 'key' on echo.
or you can do
foreach($row['strings'] as $row_string)
$key = key($row_string);
There is a bug in your code:
Given your array looks like this (as written in your question):
$row = [ "strings" => array of { "key"=> string, "value"=> string },
"simpleFilePathEscaped" => string,
"secondary_file_path_escaped" => string,
"primary_file_path"=> string
]
It is not possible for this code:
<?php foreach($row['strings'] as $row_string){
$key = $row_string['key']; // <--- THIS is not possible with the array structure you presented
$id = $row['simpleFilePathEscaped'].$key; ?>
<tr>
<td class="text-center">
<label><?php echo $key; ?> </label>
</td>
to print this as you suggested in your question:
<tr>
<td class="text-center" >
<label>direction</label>
</td>
Because during the foreach loop, the $row_string variable will hold a string value but you are trying to access it as an array with an associative key.
Related
I am trying to create a Favourites Pages where the user selects items from the "menu" and then the item is placed in the Favourites Page. I am making use of a session variable
$_SESSION['favourites']
I am starting the session by using the
session_start('session.php');
In the session_start it includes
<?php
session_start();
?>
The favourite feature works when I combine the two files together, but it does not work when I display the favourites on a different page.
This is the menu page
$conn = mysqli_connect("localhost", "root", "", "restaurant");
$sql= "SELECT * FROM menu ORDER BY id ASC";
$result=mysqli_query($conn, $sql);
$resultcheck=mysqli_num_rows($result);
if(isset($_POST["add_to_favourite"]))
{
if(isset($_POST["favourites"]))
{
$food_item_id = array_column($_SESSION["favourites"], "item_id");
if(!in_array($_GET["id"], $food_item_id))
{
$count = count($_SESSION["favourites"]);
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_desc' => $_POST["hidden_desc"]
);
$_SESSION["favourites"][$count] = $item_array;
}else{
echo '<script>alert(You have already added this item to Favourites")</script>';
}
}
else{
$food_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_desc' => $_POST["hidden_desc"]
);
$_SESSION["favourites"][0] = $food_array;
}
}
?>
<!-- Start of HTML -->
<section id="intro">
<div class="hero-global container">
<h1 class="global-header">Detailed Menu</h1>
</div>
</section>
<div class="container">
<ul class="food-cards">
<?php
if($resultcheck>0)
{
while($row=mysqli_fetch_assoc($result))
{
?>
<li class="card-info">
<form method="post" action="favourites.php?action=add&id=<?php echo $row["id"]; ?>">
<div class="food-card">
<div class="food-pic">
<img src="./assets/english-breakfast.png" alt="">
</div>
<div class="food-cont">
<h2 class="food-title">
<?php
echo $row['name']
?>
</h2>
<p class="food-desc">
<?php echo $row["description"]; ?>
</p>
<div class="price-fav-btn">
<div class="price">
<?php
echo "€".$row['price']
?>
</div>
<div class="atf-btn">
<input class="fav-btn" type="submit" name="add_to_favourite" value="Add to Favourites"/>
</div>
<input type="hidden" name="hidden_name" value="<?php echo $row["name"]; ?>" />
<input type="hidden" name="hidden_price" value="<?php echo $row["price"]; ?>" />
<input type="hidden" name="hidden_desc" value="<?php echo $row["description"]; ?>" />
</div>
</div>
</div>
</form>
</li>
<?php
}
}
?>
</ul> <!-- end of ul list -->
</div>
This is the favourites page, where the items are displayed.
<?php
if(isset($_GET["action"]))
{
if($_GET["action"] == "delete")
{
foreach($_SESSION["favourites"] as $keys => $values)
{
if($values["item_id"] == $_GET["id"])
{
unset($_SESSION["favourites"][$keys]);
echo '<script>alert("Item Removed")</script>';
echo '<script>window.location="menudetails.php"</script>';
}
}
}
}
?>
<div><h1>Favourites</h1></div>
<table>
<tr style="text-align: left">
<th width="30%">Name</th>
<th width="60%">Description</th>
<th width="5%">Price</th>
<th width="5%">Remove</th>
</tr>
<?php
if(!empty($_SESSION["favourites"]))
{
foreach($_SESSION["favourites"] as $key => $value)
{
?>
<tr>
<td width: 20%><?php echo $value["item_name"] ?></td>
<td style="text-align: left"><?php echo $value["item_desc"]?></td>
<td><?php echo '€'.$value["item_price"]?></td>
<td><span class="fav-btn">Remove</span></td>
</tr>
<tr>
</table>
<?php
}
}
?>
EDIT:
I am starting the session on both pages.
Also, the part that is not working is when it comes to seeing whats in the favourites page. There is not output, only the link (in search bar) with the respective id.
Output
I explain briefly I created a table that shows all my products on video, where inside the container that contains it are two select, enclosed in a form, which pass values in order to populate the table in my db, and everything works perfectly. The only thing though that when I select more than one product the db correctly registers only the last one selected from the checkbox,place my code in order to reach my goal, that of being able to insert more products(impianto_id_campagna),for the same customer id (cliente_id_campagna), and event id (id_campagna_cliente):
code:
<?php
$messaggio = "";
if (isset($_POST['submit'])) {
include '../connessione.php';
$id_campagna_cliente = $connessione->real_escape_string($_POST['id_campagna_cliente']);
$cliente_id_campagna = $connessione->real_escape_string($_POST['cliente_id_campagna']);
$impianto_id_campagna = $connessione->real_escape_string($_POST['impianto_id_campagna']);
$connessione->query("INSERT INTO campagne_cliente (
id_campagna_cliente,
cliente_id_campagna,
impianto_id_campagna)
VALUES (
'$id_campagna_cliente',
'$cliente_id_campagna',
'$impianto_id_campagna')");
$messaggio = "Registrazione Completata!";
}
?>
<main>
<?php
include '../connessione.php';
$query_string = "SELECT * FROM store_locator WHERE store_locator.id NOT IN (SELECT impianto_id_campagna FROM campagne_cliente)";
$query = mysqli_query($connessione, $query_string);
?>
<?php
include '../connessione.php';
$query_string = "SELECT * FROM clienti";
$clienti = mysqli_query($connessione, $query_string);
?>
<?php
include '../connessione.php';
$query_string = "SELECT * FROM campagne_cliente
INNER JOIN clienti
ON clienti.cliente_id = campagne_cliente.cliente_id_campagna
INNER JOIN campagne
ON campagne.id_campagna = campagne_cliente.id_campagna_cliente GROUP BY cognome";
$campagne = mysqli_query($connessione, $query_string);
?>
<!-- Datatables initialization -->
<script>
// Basic example
$(document).ready(function () {
$('#dtBasicExample').DataTable();
$('.dataTables_length').addClass('bs-select');
});
</script>
<!-- Structured data: Breadcrumbs -->
<form method="post" action="index.php">
<div class="container-fluid text-center">
<div class="row">
<div class="col-md-6">
<select name="cliente_id_campagna" class="ciao colorful-select dropdown-primary" multiple searchable="Cerca il Cliente">
<option value="" disabled selected>Cliente</option>
<?php
while($row = mysqli_fetch_assoc($clienti)){ ?>
<option value="<?php echo $row['cliente_id'] ;?>"><?php echo $row['nome'].' '.$row['cognome'] ;?></option>
<?php } ?>
</select>
<script type="text/javascript">
// Material Select Initialization
$(document).ready(function() {
$('.ciao').material_select();
});
</script>
</div>
<div class="col-md-6">
<select name="id_campagna_cliente" class="ok colorful-select dropdown-primary" multiple searchable="Cerca la campagna">
<option value="" disabled selected>Cliente</option>
<?php
while($row = mysqli_fetch_assoc($campagne)){ ?>
<option value="<?php echo $row['id_campagna_cliente'] ;?>"><?php echo $row['nome'].' '.$row['cognome'].' INIZIO['.$row['data_inizio'].'] FINE['.$row['data_fine'].']' ;?></option>
<?php } ?>
</select>
<script type="text/javascript">
// Material Select Initialization
$(document).ready(function() {
$('.ok').material_select();
});
</script>
</div>
</div>
<div class="col-md-12">
<?php if ($messaggio != "") echo $messaggio . "<br><br>"; ?>
<table id="dtBasicExample" class="table table-striped table-bordered table-sm" cellspacing="0" width="100%">
<thead>
<tr>
<th class="th-sm">ID
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Cimasa
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Proprietaria
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Concessionaria
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">City
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Latitudine
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
<th class="th-sm">Longitudine
<i class="fa fa-sort float-right" aria-hidden="true"></i>
</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($query)){ ?>
<tr>
<td>
<!-- Material unchecked -->
<div class="form-check">
<input type="checkbox" name="impianto_id_campagna" class="form-check-input" value="<?php echo $row['id'] ;?>" id="<?php echo $row['id'] ;?>">
<label class="form-check-label" for="<?php echo $row['id'] ;?>"></label>
</div>
</td>
<td><?php echo $row['cimasa'] ;?></td>
<td><?php echo $row['proprietaria'] ;?></td>
<td><?php echo $row['concessionaria'] ;?></td>
<td><?php echo $row['city'] ;?></td>
<td><?php echo $row['lat'] ;?></td>
<td><?php echo $row['lng'] ;?></td>
</tr>
<?php } ?>
</tfoot>
</table>
<input class="btn btn-primary" name="submit" type="submit" value="Register..."><br>
</form>
</div>
</div>
It register only the last one selected product because you have the same name attribute for every checkbox. Set the name to impianto_id_campagna[] in order to return an array in your $_POST variable.
<input type="checkbox" name="impianto_id_campagna[]" class="form-check-input" value="<?php echo $row['id'] ;?>" id="<?php echo $row['id'] ;?>">
Then you loop all your checkbox values inserting one product at a time:
<?php
$id_campagna_cliente = $connessione->real_escape_string($_POST['id_campagna_cliente']);
$cliente_id_campagna = $connessione->real_escape_string($_POST['cliente_id_campagna']);
foreach ($_POST['impianto_id_campagna'] as $value)
{
$impianto_id_campagna = $connessione->real_escape_string($value);
$connessione->query("INSERT INTO campagne_cliente (
id_campagna_cliente,
cliente_id_campagna,
impianto_id_campagna)
VALUES (
'$id_campagna_cliente',
'$cliente_id_campagna',
'$impianto_id_campagna')");
$messaggio = "Registrazione Completata!";
}
Checkbox items are passed as an array. In order to get them in to the database, you have to loop through the array and act accordingly.
foreach($impianto_id_campagna as $row){
//Iterate through and do what you need to do with the data.
}
EDIT: Solved by adding modal inside loop
Can someone explain to me how to access the foreach loop variable outside foreach?
<?php foreach($sup as $data){ ?>
<tr>
<td><i class="circular blue lock icon"></i><?php echo $data['id']; ?></td>
<td><div class="ui large green label">
Solved!
</div></td>
<td><?php echo $data['subject']; ?></td>
<td><?php echo $data['text']; ?></td>
<td><?php echo $data['date']; ?></td>
<td>
<div class="ui buttons">
<input type="hidden" name="id" value="<?php echo $data['id']; ?>" />
<input type="submit" name="cancel" value="Lock" class="ui button" tabindex="5">
<div class="or"></div>
<button id="sup" class="ui right labeled icon positive button">
Reply
</button>
</div>
</td>
</tr>
<?php } ?>
For example, I want to use $data['text'] in my modal.
Add the data attribute to your button:
<button data-text="<?php echo $data['text']; ?>" class="ui right labeled icon positive button">
Then, in your modal you can access it with Javascript, like so:
var text = document.getElementById('sup').dataset.text
Also, you can't use an unique id inside a foreach loop, otherwise it will be replicated along with the rest of the code. Remove the "id" field from your button.
Only first row is printing. Others are not printing.
Only getting a submit button in other rows
no errors or warnings found in code.
$v and $u array is same. When $v is used in both foreach nothing is printing.
<?php
foreach ($v->result() as $row)
{
?>
<tr>
<?php echo form_open('add_exam', array('role' => 'form', 'class' => 'form-horizontal')); ?>
<?php
foreach ($u->list_fields() as $field)
{
?>
<td class='hidden-1024'>
<div class="form-group">
<div class="col-sm-9">
<input class="form-control" type="text" placeholder="<?php echo $field; ?>" name="<?php echo $field; ?>" value="<?php echo $row->$field;?>">
</div>
</div>
</td>
<?php
}
?>
<td class='hidden-1024'>
<?php echo form_submit(array('name' => 'score_submit', 'id' => 'score_submit', 'value' => 'Save', 'class' => 'btn btn-embossed btn-primary m-r-20'));?>
</td>
<?php echo form_close(); ?>
</tr>
<?php
}
?>
It should be like this:
value="<?php echo $row->field;?>"
I have a query that selects all the information from a database table and puts it into an array. I then use a PHP foreach statement to display all that in a uniform manner. It's the left table here to get a sense of what I'm talking about.
What I want to do is to make one of the divs (it normally just appears repeatedly under the same name) to have a unique name for each sumbission row. For example, instead of the "response" divs all just being called response, they are "response1", "response2", and so on. Is there any way to do this? (code below)
Any help would be greatly appreciated.
Here's where I call the info from the query:
<?php foreach($images as $image) { ?>
<table id="front_pgs">
<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">
<form id="ratingform">
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/>
<input name="dislike" type="button" value='Disike' id="dislikebutton"/>
</form>
<div id="rate_count">
<div id="response">
<?php echo $image['rating'];?>
</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>
<?php } ?>
You can do this by two ways I will show you now
1- add the row id if exists to the id value or any unique column
<div id="response<?php echo $image['id']; ?>">
<?php echo $image['rating'];?>
</div>
2- make a counter
<?php
$i= 1;
foreach($images as $image) { ?>
<table id="front_pgs">
<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">
<form id="ratingform">
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/>
<input name="dislike" type="button" value='Disike' id="dislikebutton"/>
</form>
<div id="rate_count">
<div id="response<?php echo $i; ?>">
<?php echo $image['rating'];?>
</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>
<?php
$i++; //increment the $i each iteration
} ?>
<?php $i = 1; foreach($images as $image) { ?>
<table id="front_pgs">
<tr>
<td id="front_text">
<div id="imagetitle">
<?php echo $image['name'];?>
</div>
<div id="submission_info">
submitted by <?php echo $image['submitter'];?>
</div>
<div id="ratingcontainer">
<form id="ratingform">
<input name="vote" type="button" onclick="getVote('<?php echo $image['filename'];?>')" value='Like' id="likebutton"/>
<input name="dislike" type="button" value='Disike' id="dislikebutton"/>
</form>
<div id="rate_count">
<div id="response<?php echo $i; ?>">
<?php echo $image['rating'];?>
</div>
</div>
</div>
</td>
<td id="front_pg_img" valign="center" align="center">
<a onClick="switchImageUrl('<?php echo $image['filename']; ?>', '<?php echo $image['width']; ?>', '<?php echo $image['height']; ?>')"><img src="<?php echo $image['filename'];?>" id="front_pg_thumbnail"/></a>
</td>
</tr>
</table>
<?php $i ++; } ?>
Notice the $i = 1 before the foreach as well as the $i ++ before the closing }. Also, echo $i in the response div id.