Jump to content

how do i create a template page for multiple items


RaiN3772

Recommended Posts

hello there, im not sure how to explain this, also tried googling it, but nothing shows up.

 

so basically im searching for a method or a function, lets say i have multiple items in the database (item id, item_name, item description, item_price...etc)

so basically i want to create a page lets say its called "view.php" basically i'll put everything in it, connected it to the database etc, and display an item depends on the item id, so the url could be "view.php?item-id" or "view/item-id" or any other method that is available, if someone could refer me to something. not sure.

 

thanks alot

Link to comment
Share on other sites

Here's an example of the html you may need.  Study it if you don't already know it.  Look up what you don't know.

<?php
$code=<<<heredocs
	<!DOCTYPE html>
	<html>
	<head>
	<title>Untitled</title>
	<style type='text/css'>
	body
	{
		background-color:#c0c0c0;
		font-family:'Segoe UI','Trebuchet MS',Tahoma,Arial,Sans-serif;
	}
	#form_box
	{
		position:relative;
		float:left;
		width:50%;
		padding:15px;
		margin:1% 5%;
		border:1px solid blue;
	}
	.sz5 {width:5em;}
	.sz6 {width:5em;}
	.sz15 {width:12em;}
	.sz35 {width:30em;}
	</style>
	</head>
	<body>
	<div id='form_box'>
	<form method='POST' action='' autocomplete='off'>
	<label>Item id.:
	<input type='text' name='item_id' value="$item_id" class='sz5'>
	</label>
	<label>Name:
	<input type='text' name='item_name' value="$item_name" class='sz15'>
	</label>
	<br><br>
	<label>Desc.:<br>
	<textarea rows=4 cols=70 name='item_desc'>$item_desc</textarea>
	</label>
	<br><br>
	<label>Price.:
	<input type='text' name='item_price' value="$item_price" class='sz6'>
	</label>
	<center>
	<input type='submit' name='btn' value='Submit'>
	<center>
	</form>
	</div>
	</body>
	</html>
heredocs;
echo $code;
exit();

 

Link to comment
Share on other sites

Hey @ginerjm, yes i already know html, css, long time experience with it actually, but lately i been learning php, so far, i already created listing all items as cards (bootstrap cards) from the database, also submitting a new item (insert into database) but i want to create a page where i can see item details, but i dont think the right method is to create each page for each item, so thats why im asking here, i think it has to do with $_GET or something and the url should be "view.php?=item-$item_id" 

Link to comment
Share on other sites

2 hours ago, RaiN3772 said:

i think it has to do with $_GET or something and the url should be "view.php?=item-$item_id"

yes. you would create one page that accepts a $_GET variable as an input. you would test if the variables is set (see isset()), trim, validate that the value is an integer greater than zero, then securely use the value in an sql query to get the matching data to display on the page.

Edited by mac_gyver
Link to comment
Share on other sites

How about you take the div and form code and turn it into a php function.  Run a query to get your data and then fetch it row by row and call the function, passing in  the row data which will output a div/form for each record.  If you don't need the form construct and just want to display the data, change it to an html table instead of a form and dump the div too.

Link to comment
Share on other sites

23 hours ago, ginerjm said:

How about you take the div and form code and turn it into a php function.  Run a query to get your data and then fetch it row by row and call the function, passing in  the row data which will output a div/form for each record.  If you don't need the form construct and just want to display the data, change it to an html table instead of a form and dump the div too.

<?php
// Include Database Configuration File
require_once "inc/config.php";

try {
    // Select Statement
    // $sql = "SELECT * FROM items WHERE item_status = 'Approved'";
    $sql = "SELECT * FROM items inner Join users ON items.uid = users.uid WHERE item_status = 'Approved'";
    $statement = $pdo->query($sql);

    // Get all items as associative array
    $items = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$pdo = null; //close PDO connection



function index_getItems()
{
    global $items;

    foreach ($items as $items) {
        echo "<div class='d-flex align-items-sm-center mb-7'>";
        echo "<div class='symbol symbol-60px symbol-2by3 me-4'>";
        echo "<div class='symbol-label' style='background-image: url(" . $items["item_thumbnail"] . ")'></div>";
        echo "</div>";
        echo "<div class='d-flex flex-row-fluid flex-wrap align-items-center'>";
        echo "<div class='flex-grow-1 me-2'>";
        echo "<a href='#' class='text-gray-800 fw-bolder text-hover-primary fs-6'>" . $items["item_name"] . "</a>";
        echo "<span class='text-muted fw-bold d-block pt-1'>" . $items["username"] . "</span>";
        echo "</div>";
        echo "<span class='badge badge-light-success fs-8 fw-bolder my-2'>$" . $items["item_price"] . "</span>";
        echo "</div>";
        echo "</div>";
    }
}

?>

already did that to get all items, now i want to create page for item details, and pretty that the practical way to do so is not by creating each page for each item, "item-1.php" this way is efficient for alot of records, so i want a way to create a single page, and show item details depends on the $_GET method, so basically, if i go to item-1.php it should show me all the details on item ID = 1, same as 2 if go to item-2, it should show me the details about item 2...etc

 

that is the question if you get what i'm talking about

Link to comment
Share on other sites

  • 3 weeks later...
Quote

already did that to get all items, now i want to create page for item details, and pretty that the practical way to do so is not by creating each page for each item, "item-1.php" this way is efficient for alot of records

OR........... perhaps you just make a page call item.php, and you pass the id as a url parameter, query the data you need and present it.  You can also use rewrites to make it look like a static url like /item/3.  That used to be a benefit for SEO, but at this point search engines don't penalized you for having url parameters  like item.php?id=3.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.