PHP - Stuck On How To Easily Sort Items
I am having difficulty wrapping my head around how I can sort things easily.
Basically I have a few chunks of code which I will be extracting and making into includes to tidy up my code, but what I am looking for is a way to sort the includes by user preference. For instance say I have 4 include files, incude1.php though include4.php, user 1 wants them to show up 1, 2, 3, 4, user 2 wants them to show up 1, 3, 2, 4 and user 3 wants them to show up 4, 1, 3, 2.... I was thinking about using if statements but that is most definitely not the right way to go. The structure for this would be in the settings table, so they would set their preferences there, but I am stuck on how to get it to physically put things in the right order. I am sorry I have no code to show as I have absolutely no idea of where to start. I am looking for a push in the right direction (then I can post some code when it goes horribly wrong) Any help would be appreciated. Similar TutorialsHi guys, I'm just working on a script to get menu items from a database, sorted by a sort column. Each item needs to be checked to see if it has a parent, then paired with the parent. Would it be best to get all of the items at once and pair them with PHP, or get the children of each item when as a separate array? id label link parent sort 1 Home index.php 0 10 2 About about.php 0 20 3 Contact Us contact.php 2 10 Hi all ! Hi All i am wanting a column list like this http://extensions.joomla.org/extensions basically the database is set up as (id, category, parent) I want the parent to group the category section and list like the the joomla example in three columns search various threads throughout the internet but none seem to cover this entirely can any one please help????? What is the easiest way of finding out in php what date a certain day falls on? ie: I want to know what date is the 3rd Thursday of every month. for example, this month would be " The third Thursday of this month falls on : 18th November " Whats the best way of doing that in php? Thanks,,
I am trying to implement this for two days now but stuck with logics ! Please help. I have a webpage where all the database items are displyes in a table format.The table also has a check box.Upon clicking the delete button i need to delete all the items whish has the checkbox checked. How will i do that Hi ppl, I am not an php expert, but I want to allow my website users to edit a record in mySQL table. I need to allow them changing Name and description of a product, thats all. easy edit. Is there a ready component that will do the hard work for me? Thank you! Okay, I have a P value field in my Database ROW. It contains 0,1,2 0 is my About Me, 1 is my Signature Section 2 is my Friends section: Check this screenshot: Now you see that little green arrow? When you click on it, I have the code for it he Code: [Select] if (isset($ibforums->input['down'])){ if ($ibforums->input['down'] == "0"){ $new = '1,0,2'; } $DB->query("UPDATE ibf_members set p='$new' WHERE id={$ibforums->member['id']}"); header("Location: ?i={$ibforums->input['i']}"); } As you can see, it updates it 1,0,2. (Default is 0,1,2) for variable $p. Here is my code. Code: [Select] $p = explode(",", $member['p']); $middle=array ( "0" => array ( "section" => $aboutme, "b" => "banana", "c" => "apple" ), "1" => array ("section" => $signature, "b" => "banana", "c" => "apple" ), "2" => array ("section" => $f, "b" => "banana", "c" => "apple" ), ); $order = $p; print_r($middle); foreach ($order as $index) { $middlesection.= $middle[$index]['section']; } I echo out my $middlesection variable in my profile page, that's the screenshot that you see. My problem is, How can I check the input's when they click the green arrow button to know "not to go down anymore", I will be adding a up Arrow icon next to it... (I am very lost and little bit confused on how to check the input to make it all work) see like right now, on that screenshot it is: 1,0,2 (Signature,Aboutme,Friends) but if I click on that arrow again, how do I Dynamically make the 0 make sure it is the last one? but then see if I have to switch the 0 to 1,2,0 how would I check input on my 2 to know if it was the last one to move over to the second "," ?? It's just so confusing, (am I doing it wrong? it seems really confusing) But yeah... I appreciate your responses, hope you can guide,help me to get this done. <?php if(!isset($_SESSION['logged_in'])) { $_SESSION['logged_in'] = false; } if($_SESSION['logged_in'] == false) { if($_SERVER["REQUEST_METHOD"] == "POST") { $username=mysql_real_escape_string($_POST['username']); $password=mysql_real_escape_string($_POST['password']); $password2=md5($password); $sql = "SELECT * FROM users WHERE username='$username' and password='$password2'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ $_SESSION['logged_in'] = true; echo '<meta http-equiv="Refresh" content="0; url=profile"> '; } else { echo 'Sorry! Incorrect...'; } } } else { echo 'Hello '.$result[username].'The stuff you want to see! ;)'; } ?> I basicly just wanna make the user login and then show "hello, whatever your username is, the stuff you want to see", you get it. but something allways goes wrong, could someone help me with the code :s Hi guys im ben from Liverpool and im making an online store in php, and have been following a tutorial. the problem is my site so far is on wampserver. When i upload the database and php files to an online host my work just does not show. Has any1 got any advice or willing to help me? i would much appreciate it! I've been stuck too long for my beginner brain. Here is the majority of the code: Code: [Select] mysql_connect($host, $dbusername, $password); mysql_select_db($TxQuizdb); $quizquery = mysql_query("SELECT Id, Total_Questions, Maximum_Score, Passing_Score, Correct_Answer FROM Module_1_Quiz WHERE Id = '1'"); $quizqueryresult = mysql_fetch_array($quizquery); $totalq = $quizqueryresult['Total_Questions']; $maxscore = $quizqueryresult['Maximum_Score']; $passingscore = $quizqueryresult['Passing_Score']; $qvalue = $maxscore / $totalq; //scoring values $Score=0; $ansquery = mysql_query("SELECT Correct_Answer FROM Module_1_Quiz"); $QChoice_1 = $_POST['QChoice_1']; $QChoice_2 = $_POST['QChoice_2']; while ($answers = mysql_fetch_array($ansquery)) { $i++; if ($_POST['QChoice_$i'] == $answers['Correct_Answer']) { $Score = round(($Score + $qvalue), 0); } else { echo "wrong";} } The problem i am running into is trying to score this quiz with the following: Code: [Select] while ($answers = mysql_fetch_array($ansquery)) { $i++; if ($_POST['QChoice_$i'] == $answers['Correct_Answer']) { $Score = round(($Score + $qvalue), 0); } else { echo "wrong";} } I can't get the "if" statement to satisfy.....and am about to rip my eyes out... thanks! I have a table called data which stores name and other material and there is a table called match. Match has an auto incrementing id called match_id and a row called match_1 and match_2. Ok what happens is an id which is unique from the data table is stored in match_1 and another id which is unique from the data table is stored in match_2 For example Match_id match_1 match_2 40 1 2 40 2 1 Is there any way to retrive the data of both ids for instance in this case match_1 and match_2 based on the match_id being same? I tried a few joining queries but no luck for me could some one tell me how can I achieve this thanks. Basically get name place etc details of match 1 and match 2 based on the match_id Hello, I have a mysql table and when I populate it through php i have to insert blank values into fields if not it will not work. How can I put the default value in phpmyadmin to be blank, but not null. I dont understand the null checkbox or the default value option. Please help not phpmyadmin guides found Hi all,
I have 3 files, namely, [custom.js / allitem.php / fetch_pages.php]. where i have been compiling, studying and at the same time create a website of my own. I am a total newbie, so i gather examples from the entire web and try to put it together. But right now, I am stuck for 3 long days going 4 days tomorrow in a single problem whereas my head is banging on how to do it. Also been searching in internet of the same problem this has. but unfortunately, i find similar but still cant get it to work.
The codes below are currently working as I want it to be. Except from starting at this location, at [fetch_pages.php] -->
<span class="page_name"><a class="show-popup" href="#?id='.$row['id'].'">'.$row['name'].'</a></span>
Where I dont know how to retrieve the value of id='.$row['id'].' and passed it to below location and use it to query the products where id=$id.
<div class="overlay-bg" id="overlay-bg"> <div class="overlay-content"> $result = mysql_query("SELECT * FROM products WHERE id=$id", $mysqli); </div> </div> Can anyone help me code it out from this. Please.... ---------------------------------------------------------------------------------------- custom.js $(document).ready(function(){ // show popup when you click on the link $('.show-popup').click(function(event){ event.preventDefault(); var docHeight = $(document).height(); var scrollTop = $(window).scrollTop(); $('.overlay-bg').show().css({'height' : docHeight}); $('.overlay-content').css({'top': scrollTop+20+'px'}); }); $('.close-btn').click(function(){ $('.overlay-bg').hide(); // hide the overlay }); $('.overlay-bg').click(function(){ $('.overlay-bg').hide(); }) $('.overlay-content').click(function(){ return false; }); document.getElementById('data-id').innerHTML = 'data-id'; return id = 'data-id' });allitem.php <?php session_register(); session_start(); include("connect.php"); $catphp=$_GET["catphp"]; $_SESSION['catphp']=$catphp; //PAGINATION---------------------------------- $results = mysqli_query($mysqli,"SELECT COUNT(*) FROM products WHERE itemcon='$catphp'"); $get_total_rows = mysqli_fetch_array($results); //total records //break total records into pages $pages = ceil(($get_total_rows[0]/$item_per_page) + 1); //create pagination if($pages > 1) { $pagination = ''; $pagination .= '<ul class="paginate">'; for($i = 1; $i<$pages; $i++) { $pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>'; } $pagination .= '</ul>'; } //PAGINATION---------------------------------- ?> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript" src="overlay/custom.js"></script> <link href='overlay/overlaypopup.css' rel='stylesheet' type='text/css'> <script type="text/javascript"> $(document).ready(function() { $("#results").load("fetch_pages.php", {'page':0}, function() {$("#1-page").addClass('active');}); //initial page number to load $(".paginate_click").click(function (e) { $("#results").prepend('<div class="loading-indication"><img src="ajax-loader.gif" /> Loading...</div>'); var clicked_id = $(this).attr("id").split("-"); //ID of clicked element, split() to get page number. var page_num = parseInt(clicked_id[0]); //clicked_id[0] holds the page number we need $('.paginate_click').removeClass('active'); //remove any active class //post page number and load returned data into result element //notice (page_num-1), subtract 1 to get actual starting point $("#results").load("fetch_pages.php", {'page':(page_num-1)}, function(){ }); $(this).addClass('active'); //add active class to currently clicked element (style purpose) return false; //prevent going to herf link }); }); </script> </head> <body> <div id="results"></div> <?php echo $pagination; ?> </body>fetch_pages.php <?php session_start(); $catphp = $_SESSION['catphp']; include("connect.php"); //include config file ?><head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript" src="overlay/custom.js"></script> <link href='overlay/overlaypopup.css' rel='stylesheet' type='text/css'> </head> <?php //sanitize post value $page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH); //validate page number is really numaric if(!is_numeric($page_number)) { die('Invalid page number!'); } //get current starting point of records $position = ($page_number * $item_per_page); //Limit our results within a specified range. $results = mysqli_query($mysqli,"SELECT * FROM products WHERE itemcon='$catphp' ORDER BY id ASC LIMIT $position, $item_per_page"); //output results from database to a paginated window echo '<ul class="page_result">'; while($row = mysqli_fetch_array($results)) { echo '<li id="item_'.$row["id"].'">'.$row["id"].'. <span class="product-thumb"><img src="uploads/thumbs/'.$row['pic'].'"></span> <span class="page_name"><a class="show-popup" href="#?id='.$row['id'].'">'.$row['name'].'</a> </span> <br /><br /> <span class="page_description">'.$row["description"].' </span><br /><br /> <span class="page_itemcondesc">Condition : <span class="page_itemcon">'.$row["itemcon"].'</span> </span> <span class="page_warrantydesc">Warranty : <span class="page_warranty">'.$row["warranty"].'</span> </span> <br /><br /> <span class="page_price">Price:'.$row["price"].'</span> </li>'; } echo '</ul>'; ?> <div class="overlay-bg" id="overlay-bg"> <div class="overlay-content"> <font style="font-weight:bold">Product Details </font> <br /> <?php include "xx.isd"; $mysqli = mysql_connect($mysql_hostname,$mysql_user,$mysql_password); if (!$mysqli) { die("Database connection failed: " . mysql_error()); } //Select a database to use $db_select = mysql_select_db($mysql_database,$mysqli); //Perform database query to read the table entry $result = mysql_query("SELECT * FROM products WHERE id=$id", $mysqli); if (!$result) { die("Database query failed: " . mysql_error()); } //Use returned data while($row = mysql_fetch_array($result)) { $id=$row['id']; $product_code=$row['product_code']; $name=$row['name']; $description=$row['description']; $specs=$row['specs']; $accessory=$row['accessory']; $notes=$row['notes']; $itemcon=$row['itemcon']; $warranty=$row['warranty']; $price=$row['price']; $stocks=$row['stocks']; $category=$row['category']; $pic=$row['pic']; echo '<li id="item_'.$row["product_code"].'">'.$row["product_code"].' <span class="page_name">'.$row["name"].'</a> </span> <br /><br /> <span class="page_description">'.$row["description"].' </span><br /><br /> <span class="page_specs"><font style="font-weight:bold">Specification:</font> '.$row["specs"].' </span><br /><br /> <span class="page_accessory"><font style="font-weight:bold">Accessories:</font> '.$row["accessory"].' </span><br /><br /> <span class="page_notes"><font style="font-weight:bold">Notes:</font> '.$row["notes"].' </span><br /><br /> <span class="page_itemcon"><font style="font-weight:bold">Condition:</font> '.$row["itemcon"].' </span> <span class="page_itemcon"><font style="font-weight:bold">Warranty:</font> '.$row["warranty"].' </span><br /><br /> <span class="page_price"><font style="font-weight:bold">Price:</font> '.$row["price"].'</span> <span class="page_price"><font style="font-weight:bold">Stocks:</font> '.$row["stocks"].'</span> <br /><br /> </li>'; echo '<li id="item_">'.' <span class="product-thumb"><img src="uploads/thumbs/'.$row['pic'].'"></span> </li>'; } //Get images from upload_data $sql2="select file_name from upload_data where prod_name='$name'"; $result2=mysql_query($sql2,$mysqli) or die(mysql_error()); while($row2=mysql_fetch_array($result2)) { $name2=$row2['file_name']; ?> <span class="product-thumb"> <?php echo "<img src='../../uploads/thumbs/".$name2."'>"; ?> </span> <?php } //Close connection mysql_close($mysqli); ?> <br /> <button class="close-btn">Close</button> </div> </div> <?php echo '</ul>'; ?> Hi all, I am kinda stuck on the return() statement in the php.net manual about control structures. (http://au.php.net/manual/en/function.return.php) All I see is notes on when not to use it and some scary examples with loads of foo and bar but, it's hard to find out when it would be nice to use it. At the moment i am thinking its another way of echo, but I bet that's wrong. If someone could enlighten me a bit on when return() is useful I would be very happy since i see it more often everyday. CHeers! Hi: I am stuck on one little glitch with an IF ELSE statement .. hoping someone with fresh eyes can see what I am missing. The checkbox will not stay checked, and I'm over looking something but fail to see it. Code: [Select] <? if ($id != "4" ) { echo "<input type='checkbox' name='myPageActive' value='Yes' <?php if($myPageActive == 'Yes') echo 'checked'; ?>> Yes"; } else { echo "<span class='myDeleteMessage'>Homepage must stay active</span>"; } ?> It seems to have something to do with this part: Code: [Select] <?php if($myPageActive == 'Yes') echo 'checked'; ?>> Yes"; Can anyone see what I am missing? Thanks! Been trying to sort this out for a while now, had an earlier post on this as well yet haven't solved it yet. Code has changed a bit since then but still giving me trouble and I have a due date for this project is is sneaking up on me. I am trying edit previously entered information from a table which contains a student's registration information into a form. The previously entered info wont display and I am having numerous errors Here is my code Code: [Select] <?php if (!isset($_POST['sno'])) { $q = "SELECT * FROM student WHERE sno = " . $_GET['sno']; $result = mysql_query($q); $person = mysql_fetch_array($result); } ?> <h1> You are editing a student </h1> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p>Course name:</p> <select name="input1"> <?php $q = "SELECT cname FROM course "; $result = mysql_query($q); WHILE ($course = mysql_fetch_array($result)) { echo "<OPTION>{$course['cname']}</option>"; } echo " </SELECT>" ?> <br> <p>Surname:</p> <INPUT TYPE = "text" name = "input2"value= " <?php echo $person['sname']; ?>" /> <br> <p>Initials:</p> <INPUT TYPE = "text" name="input3"value="<?php echo $person['init']; ?>" /> <br> <p>Full First Name:</p> <INPUT TYPE = "text" name="input4"value="<?php echo $person['fname']; ?>" /> <br> <p>Title:</p> <INPUT TYPE = "text" name="input5"value="<?php echo $person['title']; ?>" /> <br> <p>Maiden or previous surname:</p> <INPUT TYPE = "text" name="input6"value="<?php echo $person['msname']; ?>" /> <br> <p>Date of Birth:</p> <INPUT TYPE = "text" name="input7"value="<?php echo $person['dob']; ?>" /> <br> <p>Gender:</p> Male <input type="radio" name="input8" value="m" /> Female <input type="radio" name="input8" value="f" /><br/> <br> <p>Language</p> <select name="input9"> <option >English</option> <option >Afrikaans</option> </select> <br> <p>Identity Number:</p> <INPUT TYPE = "text" name="input10"value="<?php echo $person['id']; ?>" /> <br> <p>Home Telephone Code + Number:</p> <INPUT TYPE = "text" name="input11"value="<?php echo $person['telh']; ?>" /> <br> <p>Work Telephone Code + Number:</p> <INPUT TYPE = "text" name="input12"value="<?php echo $person['telw']; ?>" /> <br> <p>Cell Phone Number:</p> <INPUT TYPE = "text" name="input13"value="<?php echo $person['cel']; ?>" /> <br> <p>Fax Code + Number:</p> <INPUT TYPE = "text" name="input14"value="<?php echo $person['fax']; ?>" /> <br> <p>E-mail Address:</p> <INPUT TYPE = "text" name="input15"value="<?php echo $person['email']; ?>" /> <br> <p>Postal Address of student:</p> <textarea name="input16" COLS=50 ROWS=5 /><?php echo $person['address']; ?> </textarea> <br> <input type= "hidden" name="sno" value="<?php echo $_GET['sno']; ?>" /> <INPUT TYPE = "Submit" name="submit" VALUE = "Update"/> </form> <?php if(isset($_POST['submit'])) { $u = "UPDATE student SET `cname` = '$_POST[input1]', `sname` = '$_POST[input2]', `init` = '$_POST[input3]', `fname` = '$_POST[input4]', `title` = '$_POST[input5]', `msname` = '$_POST[input6]', `dob` = '$_POST[input7]', `sex` = '$_POST[input8]', `lang` = '$_POST[input9]', `idno` ='$_POST[input10]', `telh` = '$_POST[input11]', `telw` = '$_POST[input12]', `cel` = '$_POST[input13]', `fax` = '$_POST[input14]', `email` = '$_POST[input15]', `address` = '$_POST[input16]' WHERE ID = $_POST[sno]"; mysql_query($u) or die(mysql_error()); echo "User has been modified!"; header("Location: index.php"); } ?> The errors are ad followed. When I open the form I get these erros and the fields are blank: Notice: Undefined index: sno in C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php on line 27 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php on line 29 When i enter new info and submit it I get these errors: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' Notice: Undefined index: sno in C:\Program Files\EasyPHP-5.3.' at line 19 And my fields contain random code e.g "<br /> <b>Notice</b>: Undefined variable: person in <b>C:\Program Files\EasyPHP-5.3.3\www\Project\editstudent.php</b> on line <b>97</b><br />" Also I dont know if this would help at all but here's the previous page which contains the code for displaying the students information and allowing to edit it, which links to the edit page Code: [Select] <?php while ($rows = mysql_fetch_array($query)): echo "<a href=\"editstudent.php?id=" . $rows['sno'] ."\" > edit </a>"; $sno=$rows['sno']; $cname=$rows['cname']; $sname=$rows['sname']; $fname=$rows['fname']; echo " <table border=1px> <tr> <td>$sno</td> <td>$cname</td> <td>$sname</td> <td>$fname</td> </tr> </table>" ; endwhile; ?> Any help would be greatly appreciated. Thanks in advance for your time and effort. Hey Guys/Gals
Benn awhile - so I'm confused on what's happening here. session_start() being called on every page, index.php, registration.php and server.php aswell as login.php. Now when I go too login - if I edit the code to echo the $_SESSION['username'] it works fine - gives me what I expect... I set the $_SESSION['username'] = $username and very next line header('Location: index.php'); this is where the issues is it's loosing the session data and I can't figure out why... so here are the two files if someone can point me in the right direction.. index.php <?php ob_start(); session_start(); if (!isset($_SESSION['username'])) { echo print_r($_SESSION); }else { echo print_r($_SESSION); } ?> <!DOCTYPE html> <html> <head> <title>Home</title> <link rel="stylesheet" type="text/css" href="styles/style.css"> </head> <body> <div class="header"> <h2>Home Page</h2> </div> <div class="content"> <!-- notification message --> <div class="error success" > <h3> <!-- there was error code display here --> </h3> </div> <!-- logged in user information --> </div> </body> </html> server.php <?php ob_start(); session_start(); ini_set('display_errors', '1'); ini_set('html_errors', '1'); error_reporting(E_ALL); //make connection with dba_close $username = ""; $email = ""; $errors = array(); $host = "localhost"; $db_username = ""; $db_password = ""; $db = ""; $conn = mysqli_connect($host,$db_username,$db_password,$db); if (mysqli_connect_errno()) { echo "Connection Failed... please contact support". mysqli_connect_error(); } //login user form details if (isset($_POST['login_user'])) { $username = $_POST['username']; //login for form initiated $user = "SELECT * FROM `users` WHERE `username` = '".$username."'"; $action = mysqli_query($conn,$user); while ($row = mysqli_fetch_assoc($action)) { if (mysqli_num_rows($action) === 1) { $_SESSION['username'] = $username; // echo "<br>"; // echo $_SESSION['username']; <-- this works echo's the username, if I remove header location redirect header('Location: index.php'); exit(); }else { echo "Username not found"; } } } ?>
Hi All i hope someone can help about to scream! basically I am trying to do a few things with the statement below; First i want to check if the user id exists in member_categories_position. If it Does i want then to exclude all entries from the second statement where member_id equals all results from the first statement the third statement is the else statement that displays if the member_id is not present in the member_categories position. PROBLEM - the result from the first system loops fine, however when i try and insert into the second statement (!='$memid') is produces no results and has no effect. I think the problem is that $memid is a looped result. How do i get the second statement to say that any member_id that is in member_categories_position will not show in that statement? Code: [Select] $sql2 = "SELECT * FROM member_categories_position a JOIN member_users b ON b.id = a.member_id"; $rs2 = mysql_query($sql2); while ($row = mysql_fetch_array($rs2)){ $memid = "".$row['member_id'].""; } if(mysql_num_rows($rs2) != 0){ $new= "SELECT * FROM member_categories JOIN member_users on member_categories.member_id=member_users.id JOIN member_config on member_categories.member_id=member_config.member_id WHERE member_categories.categories='$category' and member_categories.member_id !='$field' group by member_config.member_id order by RAND() limit 0,42"; $rs = mysql_query($new); while ($row = mysql_fetch_assoc($rs)) { echo "result excluding member ids from the first statement"; } echo "<div class=\"clear\"></div>"; } else{ $new= "SELECT * FROM member_categories JOIN member_users on member_categories.member_id=member_users.id JOIN member_config on member_categories.member_id=member_config.member_id WHERE member_categories.categories='$category' group by member_config.member_id order by RAND() limit 0,42"; $rs = mysql_query($new); while ($row = mysql_fetch_assoc($rs)) { echo "Result with all member ids"; } echo "<div class=\"clear\"></div>"; }} |