PHP - Separating The Backend From The Frontend.
I'm going through my project trying to find a way of separating the backend (the query and php code) from the frontend (html/css). I want to bring the two together with the include function. I've done other but I'm getting confused because the while loops html code. I don't want someone to do this for me, I just what someone to highlight the most logical way.
<?php include 'auth.inc.php'; include 'db.inc.php'; include 'buy.inc.php'; //connect to database. $db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect. Check your connection parameters.'); mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db)); //select orders that this user has bought. $query2 = sprintf('SELECT d.name_id, d.order_id, d.order_qty, d.product_code, feedback, p.title, c.email FROM order_details d LEFT JOIN product p ON d.product_code = p.product_code LEFT JOIN contact c ON d.name_id = c.name_id WHERE d.buyer_id = "%u"', mysql_real_escape_string($_SESSION['name_id'])); $result2 = mysql_query($query2, $db) or die(mysql_error()); //echo out the information in a while loop, add pagination? $odd = true; while ($row2 = mysql_fetch_array($result2)) { echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">'; $odd = !$odd; echo '<td style="text-align: center; width:100px;">' . $row2['title'] . ' <table> <tr> <th colspan="2">Shipping Information</th> </tr><tr> <td>First Name:</td> <td>' . $row2['title']. '</td> </tr><tr> <td>First Name:</td> <td>' . $row2['order_qty']. '</td> </tr><tr> <td>Last Name:</td> <td>' . $row2['product_code']. '</td> </tr><tr> <td>Billing Address:</td> <td>' . $row2['email'] . '</td> <td>' . $row2['order_id'] . '</td> </tr> </table> </tr>'; if ($row2['feedback'] == 0){ echo ' <form action="reputation.php" method="POST"><p>Please give the seller feedback on how you have received your product. <td><input type="hidden" name="name_id" value="' . $row2['name_id'] . '"></td> <td><input type="hidden" name="order_id" value="' . $row2['order_id'] . '"></td> <td><input type="submit" name="submit" value="Feedback"/></td> </form>'; } } ?> Similar TutorialsHi guys! I am working to create a new front end for WHMCS using the backend. Here is what I have so far:
http://inzernettechn...s.com/login.php
http://inzernettechn...om/register.php
http://inzernettechn.../clientarea.php
http://inzernettechn...setPassword.php
e-mail: cody-admin@inzernettechnologies.com
password: cmr432we
(To login to the login and to get to the client area, I know it isn't much but this is what I got done in a couple hours)
I am wondering if someone would like to help with this project with me
I was planning on selling this, so you could get some of the money from sales if you want. If you are interested, please let me know! Thanks!
Hi, I m creating a website (Wordpress) for my client. I need a small help. I am trying to find a good IDE for PHP and JavaScript. I fiddled around with Visual Code and it was very buggy. The GitHub page said over 800 open errors?!?!? Now I'm looking at PHPStorm and it reads that only PHP in a PHP page or JavaScript in a JavaScript page may be debugged. I am wondering if this is always possible to separate the code like this.
Also, in the following code, what do you all think about using a XMLHttpRequest() to call this right in the beginning of the page with no button or anything. Is this bad form? It creates a dropdown list from the database. Right now it just sits there. This code isn't ready for prepare yet.
<?php $host = 'localhost'; $user = 'root'; $pass = ''; $database = 'ecommerce'; $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false ); $dbo = new PDO("mysql:host=$host;dbname=$database", $user, $pass, $options); $q4 = "SELECT categories.title FROM categories INNER JOIN customers on customers.CustomerID = categories.CustomerID and customers.CustomerID = 1"; echo '<select id = "dropDown1" >'; foreach ($dbo->query($q4) as $row) { echo "<br>"; echo '<option value = " '; echo $row['title']; echo '">'; echo $row['title']; echo '</option>'; } echo "<br>"; echo '</select>'; ?> I'm using AJAX.
Edited January 2 by JoshEir Hi guys, in my database i have the table called users, where i have 5 fields (id, username, email, password, user_level) - for the user_level field i have 2 options administrator and editor.
What i want to do is that when the user who is logged in have administrator in the user_level field to see all the pages from backend, and the user who have in the user_level field editor to see only some of the pages from the backend such as newsletter, or messages.
I hope you understand what i'm asking if not fell free to ask me if you need more specific details.
I tried to make a php page called access.php wher i put the following code, but not working
<?php session_start(); $sql = $mysqli->query("SELECT user_level FROM imobiliare_users WHERE id=$id"); $user_level = $mysqli->query($sql); echo $user_level; if ($user_level !="administrator") { echo "You are not the proper user type to view this page"; die(); } ?>Hope you can help me. Thx in advance for help. Hey Guys, Here is my issue. I have a MySQL database with a table of products. I use this table to generate a mutliselect form element that is used int a larger form. The larger form contains other information relevant to an issue that we are having with that product (such as an outage). Anyway, the form works fine and the form validation works fine and I write it to the database fine. The issue I'm having is how to save the mutliselect options most efficiently. This is my current process: 1. User creates a new entry from the form and clicks save 2. The form is validated 3. All entries are written to the database in a table called "incidents" (content inclues, timestamps, description of the problem, the impact, who to contact, etc) 4. Because the products list can vary from 1 product to all (~30) products I broke it out into a separate table called "impacted_products". I then loop through each of the selected products and write one row for each product using the id from the row in the "incidents" table to define the tables relationship. Example of DB results: "incidents" +-----------------+------+----------+---------------------+---------+---------+ | notification_id | what | impact | time_start | summary | contact | +-----------------+------+----------+---------------------+---------+---------+ | 235235 | Test | None.... | 2011-02-08 13:41:00 | Test | 3 | +-----------------+------+----------+---------------------+---------+---------+ "impacted_products" +-----+-----------------+------------+---------------------+---------+ | id | notification_id | product_id | timestamp | deleted | +-----+-----------------+------------+---------------------+---------+ | 202 | 235235 | 7 | 2011-02-10 14:30:42 | 0 | | 203 | 235235 | 37 | 2011-02-10 14:30:42 | 0 | | 204 | 235235 | 23 | 2011-02-10 14:30:42 | 0 | +-----+-----------------+------------+---------------------+---------+ 5. Now the users wants to go back and make some updates. 6. They click the edit button from the menu on screen and the form is brought back up and all options/fields are filled out from that which is stored in the database. 7. They make their edits and save again. 8. Now this time it simple updates the "incidents" table but for the products table I do a: "UPDATE impacted_products SET deleted=1 WHERE notification_id='$ID'" and "delete" all the old impacted_products for this particular incident and then loop through all the products that the user still had selected and write them to the database again. 9. And repeat So as you can see this could end up with a lot of "useless" entries in the database since my scripts only pay attention to those "impacted_products" whose deleted value is set to 0. Example: +-----+-----------------+------------+---------------------+---------+ | id | notification_id | product_id | timestamp | deleted | +-----+-----------------+------------+---------------------+---------+ | 176 | 235235 | 37 | 2011-02-08 15:26:25 | 1 | | 177 | 235235 | 43 | 2011-02-08 15:26:25 | 1 | | 178 | 235235 | 37 | 2011-02-08 15:37:58 | 1 | | 179 | 235235 | 43 | 2011-02-08 15:37:58 | 1 | | 180 | 235235 | 1 | 2011-02-08 15:39:49 | 1 | | 181 | 235235 | 7 | 2011-02-08 15:39:49 | 1 | | 182 | 235235 | 37 | 2011-02-08 15:39:49 | 1 | | 183 | 235235 | 43 | 2011-02-08 15:39:49 | 1 | | 184 | 235235 | 1 | 2011-02-08 15:40:53 | 1 | | 185 | 235235 | 7 | 2011-02-08 15:40:53 | 1 | | 186 | 235235 | 37 | 2011-02-08 15:40:53 | 1 | | 187 | 235235 | 43 | 2011-02-08 15:40:53 | 1 | | 188 | 235235 | 37 | 2011-02-10 10:00:47 | 1 | | 189 | 235235 | 1 | 2011-02-10 12:17:05 | 1 | | 190 | 235235 | 7 | 2011-02-10 12:17:05 | 1 | | 191 | 235235 | 13 | 2011-02-10 12:17:05 | 1 | | 192 | 235235 | 37 | 2011-02-10 12:17:05 | 1 | | 193 | 235235 | 23 | 2011-02-10 12:17:05 | 1 | | 194 | 235235 | 1 | 2011-02-10 12:21:52 | 1 | | 195 | 235235 | 7 | 2011-02-10 12:21:52 | 1 | | 196 | 235235 | 13 | 2011-02-10 12:21:52 | 1 | | 197 | 235235 | 37 | 2011-02-10 12:21:52 | 1 | | 198 | 235235 | 23 | 2011-02-10 12:21:52 | 1 | | 199 | 235235 | 7 | 2011-02-10 12:22:26 | 1 | | 200 | 235235 | 37 | 2011-02-10 12:22:26 | 1 | | 201 | 235235 | 23 | 2011-02-10 12:22:26 | 1 | | 202 | 235235 | 7 | 2011-02-10 14:30:42 | 0 | | 203 | 235235 | 37 | 2011-02-10 14:30:42 | 0 | | 204 | 235235 | 23 | 2011-02-10 14:30:42 | 0 | +-----+-----------------+------------+---------------------+---------+ I did it this way beacuase it seems the easiest and fastest way. Otherwise I would have to lookup all the impacted_products from the table that match that notification_id and check 1) Was there a new product selected? If so, add it. 2) Was a product that was selected no longer selected? If so, delete it. 3) Was a product that was selected before still selected now? If so, leave it alone. This seemed like a lot of extra looping and a lot of extra DB queries to essentially end up at the same place. However, I feel that there has still got to be a more efficient way of doing this where I won't have all the extra entries in the impacted_products table. Any ideas? Thanks in advance! I'm trying to separate the HTML(form) into a separate .html file (from the php file below). Someone suggested to "place the HTML in someFile.html and the PHP in someFile.php, and alter the <form> tag's 'action' element in the .html file to target someFile.php". But I don't know how to alter the <form> tag's 'action' element in the .html file to target this upload_file.php file. Any additional help will be appreciated. <?php session_start(); require_once 'phps3integration_lib.php'; $message = ""; if (@$_POST['submit'] != "") { $allowed_ext = array("gif", "jpeg", "jpg", "png", "pdf", "doc", "docs", "zip", "flv", "mp4"); $extension = end(explode(".", $_FILES["file"]["name"])); if (($_FILES["file"]["size"] < 32428800) && in_array($extension, $allowed_ext)) { if ($_FILES["file"]["error"] > 0) { //$message.="There is some error in upload, see: " . $_FILES["file"]["error"] . "<br>";//Enable this to see actual error $message.="There is some error in upload. Please try after some time."; } else { $uploaded_file = uploaded_file_to_s3($_FILES["file"], "uploads", true); if ($uploaded_file != FALSE) { $user_name = @$_POST['user_name'] != "" ? @$_POST['user_name'] : "Anonymous"; $form_data = array( 'file' => $uploaded_file, 'user_name' => $user_name, 'type' => 'file' ); mysql_query("INSERT INTO `phps3files` (`id`, `file`, `user_name`, `type`) VALUES (NULL, '" . $uploaded_file . "', '" . $user_name . "', 'file')") or die(mysql_error()); $message.= "File successfully uploaded in S3 Bucket."; } else { $message.="There is some error in upload. Please try after some time."; } } } else { $message.= "Invalid file, Please upload a gif/jpeg/jpg/png/pdf/doc/docs/zip file of maximum size 30 MB."; } } ?> <?php require_once 'header.php'; ?> <fieldset> <legend>PHP AWS S3 integration library Demo1</legend> Description: In this demo a file is being upload to an S3 bucket using "PHP AWS S3 integration library". After upload you can check the uploaded file in below table. If you require some manipulation before uploading file to S3 then check <a href="upload_file_manually.php">Demo2</a> <br /> <br /> <form action="" method="post" enctype="multipart/form-data"> <div class="control-group"> <label for="file" class="control-label">Choose a file to upload: <span style="color:red">*</span></label> <div class='controls'> <input id="file" type="file" name="file" /> <?php //echo form_error('file'); ?> </div> </div> <div class="control-group"> <label for="user_name" class="control-label">Your name:</label> <div class='controls'> <input id="user_name" type="text" name="user_name" maxlength="255" value="" /> <?php //echo form_error('user_name'); ?> </div> </div> <div class="control-group"> <label></label> <div class='controls'> <input type="submit" name="submit" value="Submit" class="btn"> </div> </div> </form> </fieldset> <?php if ($message != "" || @$_SESSION['message'] != "") { ?> <div class="alert alert-success"> <?php echo $message; ?> <?php echo @$_SESSION['message']; @$_SESSION['message'] = ''; ?> </div> <?php } ?> <div> <table class="table table-hover"> <caption> <strong>Last 10 user uploaded files</strong> </caption> <?php $files_result = mysql_query("SELECT * from `phps3files` WHERE type LIKE 'file' ORDER by id DESC LIMIT 10"); $i = 1; while ($file = mysql_fetch_object($files_result)) { ?> <tr> <td><?php echo $i++; ?></td> <td><a href="<?php echo site_url_s3("uploads/" . $file->file); ?>" target="_blank">View/Download</a> </td> <td><a href="<?php echo site_url("delete_file.php?id=" . $file->id); ?>">Delete file from S3</a></td> <td><?php echo "Uploaded by: " . $file->user_name; ?></td> </tr> <?php } if ($i == 1) { ?> <tr> <td colspan="2"> No files uploaded yet</td> </tr> <?php } ?> </table> </div> <h4>Source Code Part of Demo</h4> <pre class="prettyprint lang-php linenums"> <?php session_start(); require_once 'phps3integration_lib.php'; $message = ""; if (@$_POST['submit'] != "") { $allowed_ext = array("gif", "jpeg", "jpg", "png", "pdf", "doc", "docs", "zip"); $extension = end(explode(".", $_FILES["file"]["name"])); if (($_FILES["file"]["size"] < 32428800) && in_array($extension, $allowed_ext)) { if ($_FILES["file"]["error"] > 0) { //$message.="There is some error in upload, see: " . $_FILES["file"]["error"] . "<br>";//Enable this to see actual error $message.="There is some error in upload. Please try after some time."; } else { $uploaded_file = uploaded_file_to_s3($_FILES["file"], "uploads", true); if ($uploaded_file != FALSE) { $user_name = @$_POST['user_name'] != "" ? @$_POST['user_name'] : "Anonymous"; $form_data = array( 'file' => $uploaded_file, 'user_name' => $user_name, 'type' => 'file' ); mysql_query("INSERT INTO `phps3files` (`id`, `file`, `user_name`, `type`) VALUES (NULL, '" . $uploaded_file . "', '" . $user_name . "', 'file')") or die(mysql_error()); $message.= "File successfully uploaded in S3 Bucket."; } else { $message.="There is some error in upload. Please try after some time."; } } } else { $message.= "Invalid file, Please upload a gif/jpeg/jpg/png/pdf/doc/docs/zip file of maximum size 30 MB."; } } ?> </pre> <?php require_once 'footer.php'; ?> Lets say I pull a value from a database field that has a value of 1,2 and what I want to do is seperate them and then run each of those values against a separate table how would that be accomplished. I'm having trouble trying to separate the output into variables that I can use/echo on my page. when I do a print I see the two rows of data all grouped together how can I separate each result base on field and row? maybe something like $line['m_id'][0], $line['m_name'][0], $line['m_id'][1], $line['m_name'][1], etc... $bio = mysql_query("SELECT * FROM soc_meminfo WHERE m_id = '".mysql_real_escape_string($en['mm_id'])."'"); if (mysql_num_rows($bio) == 0) call404(); while ($line = mysql_fetch_assoc($bio)) { foreach ($line as $key => $value) { $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value)); } echo '<pre>'; print_r($line); echo '</pre>'; } Hi there, I'm new to the board, so apologise if something like this has been asked before. I'm also quite new to php, so the disaster that is my code will probably make your stomach turn, but it works so far... I am using file_get_contents to grab a website I have access to (my work rota, incidentally), and parse a bit of the html. The aim is to be able to take my rota and do a few things with the times and dates, etc.. So far, I have got the file, parsed the crap out of the string that I don't need, and i'm left with something that looks like this: Code: [Select] <TR><TD ></TD></TR><TR bgcolor='ffff00'><TD>Date</TD><TD>Duty</TD><TD>Dep</TD><TD>Begin</TD><TD>End</TD><TD>Arr</TD></TR><TR><TD ></TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>297</TD><TD>STN</TD><TD>15:55</TD><TD>17:10</TD><TD>DUB</TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>288</TD><TD>DUB</TD><TD>17:35</TD><TD>18:50</TD><TD>STN</TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>293</TD><TD>STN</TD><TD>19:15</TD><TD>20:30</TD><TD>DUB</TD></TR><TR><TD>23 Apr 12, Mon</TD><TD>298</TD><TD>DUB</TD><TD>20:55</TD><TD>22:05</TD><TD>STN</TD></TR><TR><TD ></TD></TR> The people who wrote my rota weren't very tidy, but in a nutshell, I want the information between the TD/TR brackets. Each Row contains cells with my date, time, destination etc in it. I can use striptags, but I end up with a long string that is difficult to split into useful information. I need something that parses the string like this.. "for every row grab information between <td> and </td> and load into $td[0]" etc.." How would I go about doing something like this? I'm a bit stumped. Thanks in advance Horgy Can someone give me some guidance of how to separate Form Display from Form Processing? I have always used forms that submitted back to themselves which isn't so bad, but then trying to cram in code to display the form, validation errors, and messages after the form is processed all in one file is insane?! Currently I am working on a simple "Add a Comment" form. It would be nice to have a separate form processing script, but I don't know where to begin... Debbie Hi Guys I'm returning to programming after 5 years, almost a newbie. I have a field(column) in a MySQL database table that has strings of characters separated by a delimiter (||). For example: (TOM||PAUL||HARRIS) I would like to separate the strings into an array. Something like this: ARRAY[0] = 'TOM'; ARRAY[1]='PAUL'; ARRAY[3]='HARRIS'; Can some one tell me how to do this? TIA. This is a LINK to the page -> http://shoutkey.com/carriage I have a problem of separating MySQL concatenated info, yet still maintaining their relationship to other arrays in the same order. First off here is the MySQL code: Code: [Select] $query ="SELECT report,"; $query.="GROUP_CONCAT(DISTINCT docID) AS docIDs, "; $query.="GROUP_CONCAT(DISTINCT analyst) AS analysts, "; $query.="GROUP_CONCAT(DISTINCT region) AS regions, "; $query.="GROUP_CONCAT(DISTINCT country) AS countries, "; $query.="GROUP_CONCAT(DISTINCT topic) AS topics, "; $query.="GROUP_CONCAT(DISTINCT date) AS dates, "; $query.="GROUP_CONCAT(DISTINCT event) AS events, "; $query.="GROUP_CONCAT(DISTINCT link) AS links, "; $query.="GROUP_CONCAT(DISTINCT province) AS provinces "; $query.="FROM reports GROUP BY report ORDER BY dates DESC, docIDs DESC"; The most important thing is to get all attributes about each report. The thing is, each report can have as many records as it must, in order to store various types of information, like, if multiple analysts are working on the report, then each one saves their own record, but when retrieving these records MYSQL concatenates the records, to make it look to the analysts like this Report 1 --- Josh, Rebecca --- Philippines However, my problem is trying to display the reports by country... as you can see http://shoutkey.com/carriage. I have no idea about how to split up the countries, while maintaining their relationship to the $rows pulled from MySQL. I am gonna post the entire code, and let me know if anybody has any ideas. Code: [Select] $result = mysql_query($query); $numrows = mysql_affected_rows(); $current = false; while($row=mysql_fetch_assoc($result)) { $dates = $row['dates']; $analysts = $row['analysts']; $provinces = $row['provinces']; $topics = $row['topics']; $regions = $row['regions']; $countries = $row['countries']; $reports = $row['report']; $links = $row['links']; $events = $row['events']; if (!$current){ //first call echo "<h3><a href=\"#\">{$countries}</a></h3>"; //echo first call $current = $countries; echo "<div>" ;//open content div } elseif($current != $countries){ // country has changed echo "</div>"; //close previous content div echo "<h3><a href=\"#\">{$countries}</a></h3>"; //echo subsequent countries $current = $countries; echo "<div><p>" ;//open content div } echo $analysts." ----- ".$provinces." ----- ".$reports; echo "<br /><br />"; } echo "</div>"; ?> </div> Hi there, In a string for a shopping cart I am storing both the item id and the item size selected in a number like this 12s1 where 12 is the id and 1 is the size. How can I separate these from one another again? I'm guessing it's to do with expand() ? Thanks, Jack |