Print multiple PHP Files whitout opening them - php

I have a PHP system that have orders, and items related to orders,
So I want to print every item related to specific order, without open the file.
I'm using a SQL query to show the pages:
SELECT * FROM items WHERE order_id = ".$oid.";
to collect items from this order and for each one, I write
<a href="view.php?iid=<?php echo $theResult['id'];?>" target="_blank">
Item <?php echo $theResult;?>
</a>
So the page view.php is a default that receive item_id in $_GET and build the page as I want.
There's no problem until here, BUT:
I want to print any result for view.php, for each item in order, without have to open and click to print everyone.

If you insist on still using the single view file view.php, a bit of a hacky solution could look like this:
<?php
foreach ($items as $iid) {
$_GET["iid"] = $iid;
require("view.php"); //This will print whatever the view.php page does
}
?>
Here $items specifies an array full of the ID's from the database.
By setting $_GET["iid"] to the id which we'd get from our database ($iid) and then including view.php, you can emulate a call to the file (and hence we'd print the view.php page for every item in the database).
If this is not what you want, it'd be awesome with some more info so we can help you along the way.

Related

How do i get make session id from a loop of mysqli?

I am learning php and i need some help in one of my project.
How can i get id of a specific row while displaying the value of other column (in my case title) from a loop so that i can make it $SESSION and forward it to next page.
For now what i want to do is get an id of the row when i click the title and show the post in the next page and work further on it. I think i can use javascript for but don't know how to do so.
My codes (by which i am currently fetching data from database)
<div class="list-group">
<?php while($row = mysqli_fetch_array($search_result)):?>
<?php echo $row['title'];?>
<?php endwhile;?>
</div>
Thanks in Advance
Assume view_page.php is the page you want to view content of that row on when you click the row and you will use get method to extract id to go fetch content of a specific row in your database.
" class="list-group-item list-group-item-action list-group-item-dark">
on url address you will see something like after view_page.php?id=1
use this coode to get that row id: $id = $_GET['id'];

PHP How to stay on same page after another form has been processed?

So on this application I'm working on, it searches for some code in my database, grabs it, opens a new page and places the code (where necessary) onto the new page by getting its id.
Now within that code (from the database) there is another form that is processed based on user's input. The problem that I am getting is that the new form is not processed on the same page. It redirects to a new url that doesn't specify the code from the database. For example...
When the new page is generated with the code from the database, the url looks like this...
localhost/newpage.php?id=1
Then when I submit the form within the code from the database it changes to this...
localhost/newpage.php?input1=blah&input2=blah
But I want something like this...
localhost/newpage.php?id=1&input1=blah&input2=blah
Just FYI this code needs to be dynamic. For example, let's say I don't know what id the user is looking for and within that id I don't know how many input fields there are.
If you guys need some explicit code (obviously I left out the unnecessary things)...
This is searchpage.php which searches db and displays all relevant items. Then the user selects an item which contains an id and generates newpage.php with that id...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
This is the form retrieved from database and placed in newpage.php...
<form action="newpage.php" method="GET">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
This is the php at the top of newpage.php when it is generated...
if (isset($_GET['id'])) {
//retrieves data from specified id from database
if(isset($_GET['inputs'])) {
//do something with user inputs
}
}
Is there any way to achieve this? All help is appreciated! Thanks :)
SOLUTION
For this to work, I had to create a session which stores the id. Consequently i had to change all my $_GET varaibles (that dealt with id info) to $_SESSION variables. But it works as long as you're not storing critical/sensitive info!
Here's the code changes...
Changes in search.php ...
//there is a search query then puts all elements in an associative array
$rows = items->fetch_all(MYSQL_ASSOC);
foreach ($rows as $row) {
$id = $row['id'];
$_SESSION['current_id'] = $id;
echo "<a href='newpage.php?id=$id>User clicks to generate a newpage</a>";
}
Changes in newpage.php ...
<form action="newpage.php?id=" method="POST">
<input name="input1"></input>
//there can be an x amount of input tags here depending on what id is pulled
</form>
Changes in php of newpage.php ...
if (isset($_SESSION['current_id'])) {
//retrieves data from specified id from database
if(isset($_POST['inputs'])) {
//do something with user inputs
}
}
ALL THANKS TO...
Sathik Khan
You can get the query string "id" in referred header. Either you can attach that query string in client or in server.
Please use $_SERVER['HTTP_REFERER'] to get referer url and from there you can get the id. If it's id of logged in user, I would recommend store them in session for security reason.
Based on your comments,
You can take any one of these actions,
1. Keep the ID in your form as hidden variable
2. Update your url with id as one of the query string and send back to client for error correction.
3. Keep the ID in session, if applicable
4. Use post and perform form validation before redirecting
Thanks.

opencart Need to show option quantity in admin panel per product in

I really need this as most of my products have options in Opencart. I need to show option quantities for each product in the admin list.
At the moment in the quantity column it just shows a number. It would be better if it said for example Strawberry: 12 Vanilla: 5.
Can someone please help me?
The first thing you should do is see if there is an extension for this on the OpenCart extension page. If there is no extension then this is what you will need to do. 1) Find which database table this information is in (I think its product). Then query it in your model , pass it to your controller, and then echo that variable/ array in your view. I will provide you roughly with the code you will need, however you need to tweak it to do what you want.
This should go in the appropriate Model file:
public function getProductName() {
$query = $this->database->query("SELECT * product_name FROM product"); /*this gets all the info you want, however I am not %100 on the names, so double check your database*/
if (isset($query){ /*says if array is not NULL and is set*/
return getProductName(); /* return the function */
}
else {$query = ""} /*else the array is NULL*/
{
This should go in your Controller:
$this->load->model('path/to/model/file') /*however you might not need this depending on which model file you place your query (model code)*/
$this->data['productName'] = $this->path_to_model->getProductName(); /*this passes it to your view file as the array productName*/
This should go in your View:
You will see certain HTML tags and foreach loops on your view page, you will want to basically copy the view code for displaying the array properly, however it should look something like this:
<tr>
<td>
<?php foreach ($productName->rows as $tempVariable) { /*this is roughly how you will get each product name from database table to print on a page, obviously if you want to display it next to its quantity you will need to change it around by using HTML and PHP code in conjugation, however I hope this has helped*/
echo $tempVariable; ?>
</td>
</tr>

Dynamic page creation PHP + MySQL

Been struggling with this problem for a while now and I just can't het my brain to understand it.
I have a very simple website where I can add items in a database. There is a list on index.php where the list is displayed and each item needs a url that directs to a "more information" page.
The "more information" page has to be a dynamic one as there are a lot of items and these items can be added or deletend.
What my code for this section looks like at the moment:
$result_set = mysql_query("SELECT id, name FROM items WHERE id = $item");
while ($item = mysql_fetch_row($result_set)) {
$name = $item['name'];
echo "$name";
This results in a link if item 1 = wrench ../items/wrench.html.
But this page obviously doesn't excist. How can I get this to work?
You can't have one html page for each item if the items are dynamically added
But you can do it this way
echo '$name';
This way you have only one page who receive as a GET parameter the id of the item you want the description.
In the page of more_information.php you just display a text corresponding to the id you received.
use .htaccess for this.
you can add this code
RewriteEngine On
RewriteRule ^items/(.+)$ items.php?page=$1
In your items.php use $items=$_GET[page]; so you can read what is in url after items/
This is a link where you can find more about RewriteRule's http://httpd.apache.org/docs/current/rewrite/intro.html

Php inside a javascript function?

I have a feature on my users inbox that allows users to check/uncheck messages in their inbox that they want to make favourite.
I'm currently testing what happens when a user checks the box (clicks on the image and causes it to go from greyed out to colour meaning the box is checked).
Anyway as you can see from the code below when the box ischecked this url is suppose to be loaded: http://mysite.com/messages/favourite_checked
The message_id of the row the user has checked the box on is suppose to be added onto the end of the url this then loads my controller "messages" and method "favourite_checked" which then passes a variable that grabs the message_id from the url, stores it in a variable then sends it the my model and it is used in a mysql query.
Basically I update the favourites column of my messages table and set it to = 1 where the message_id from url matches the one in the messages table in my database. So yea, where the match is found the "favourite" column in that row is updated to 1. 1 = favourite 0 = not favourite.
Any I just thought I would make it clear what was happening..
My problem is nothing happens when I check the box, nothing is updated so I feel I must be doing something wrong where I try to add the id to the url in the javascript function.
I've tried $(post) also.. nothing happens then also.
Maybe someone can spot it because I really don't know what the problem is.
<script type="text/javascript">
// favourite check box
$('input.favourite:checkbox').simpleImageCheck({
image: '<?php echo base_url()?>images/messages/check.png',
imageChecked: '<?php echo base_url()?>images/messages/unchecked.png',
afterCheck: function(isChecked) {
if (isChecked) {
//query to db from php to update favourite number to 1
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
}
// else (!isChecked)
// {
// //query to db from php to update favourite number to 0
// $.get('http://mysite.com/messages/favourite_unchecked');
// }
}
});
</script>
I think your basic problem is some confusion about when the PHP is running vs the javascript.
The PHP you put on the page is server side, it will load first, then the javascript will run client-side.
This part here:
$.get('http://mysite.com/messages/favourite_checked'+'<?php foreach ($query as $row): ?><?php $row['id']; ?><?php endforeach; ?>');
Seems like you are wanting this to be dynamic based on what you checked, but I don't see how that url is going to show specifically what you are looking for.
About the PHP:
I think you want to replace this:
<?php $row['id']; ?> // does nothing
with this:
<?php echo $row['id']; ?> // echo's the id
Although I´m not sure that that will work as the loop you have there will generate a strange url, just adding all id's...
About the javascript:
I´m not familiar with the simpleImageCheck() function you are calling, but does it have an onClick or onChange event handler? Otherwise I don´t see your code being run at all.

Categories