PHP - Save Key+value In Array In Order To Remember Current Page Per User
Hey all..
I'm trying to pick up PHP by reading ebooks and follow tutorials. I am now busy with creating a thumbnailgallery from uploaded pics per user. I want to let the visitor scroll through a row of thumbnails by clicking next or previous. There are 3 different rows of thumbnails. Here's how it looks like: <<previous (row1 of 6thumbnails) next>> <<previous (row2 of 6thumbnails) next>> <<previous (row3..... ) next>> when page loads mysql fetches rows of information for the filenames...then there is a foreach row loop that does first a do ..while loop to build up the thumbnailrows...and then creates the navigation links previous and next who send the "page"+1 or "page"-1 to _GET array so that the thumbnail 7 until 12 will be shown. The problem i run into is that i want the main code page to remember at which "page" of scrolling through the thumnails he is. Now it is that when a user scrolls one row of thumbnails and stops at the 3rd "page" of it and then clicks on a different row to go to "page" 2 of that row that all the rows jump together back to "page" 1. I thought there may be a solution in storing the number of row and the number of page into an array but can't seem to find a solution.... Has anybody got experience in this? Help would be very welcome i'm totally stuck and cant read about it in tutorials/ebooks.... greets from holland.. GJ Similar Tutorialsi get this error Warning: current() [function.current]: Passed variable is not an array or object.. for this Code: [Select] $lastblock = current( ${"s".$row} ); print_r($lastblock); when i change to this it works.. Code: [Select] $lastblock = current( $s0 ); print_r($lastblock); The problem is i won't know the $row seeing as it is in a while loop. Solution? I am working on a site that is going to have hundreds of polls that people can vote on. Once any user creates a new poll (vote: Hayden Panettiere vs. Kristen Bell as an example) I want to have the database remember what member voted on what, for each poll. So user 1 voted Hayden, user 327 voted Kristen.. etc.. I expect up to 500 users to vote on each topic, but a new column to store each username that voted seems insane. I was thinking arrays, but am not sure if that would work. I don't think that a new table per poll would be very efficient either, it would be like 500++ tables on one database. To explain better, my username is Paradox and I vote for Kristen Bell. When I look at this database I want to see: ID Title Option1 Option2 People who voted option 1(27 people) People who voted option 2(783 people) 1 Heroes 27 783 {Blah, Blah, Blah} {Paradox, Blah, Blah} Is an array a good way to go about this? Because I also want to be able to expand the array to notify each member they voted with the winning or losing team once the poll expires. I would like to know how to take details of my 'users' table from mysql database and check if that user is logged in using cookies or sessions somehow and have that users username displayed on my site, something like: user logged in as: (username), how is this done? What would you guys need to see in order for you to help me? im guessing the login and register scripts which are below, if you need anymore let me know, thanks. Login script: Code: [Select] <?php if(isset($_POST['submit'])){ //check for submit button click $username = $_POST['myusername']; $password = $_POST['mypassword']; // proceed with rest of code $self = $_SERVER['PHP_SELF']; $referer = $_SERVER['HTTP_REFERER']; #if either form field is empty return to the log-in page if( (!$username ) or (!$password ) ) { header( "Location:$referer" ); exit(); } #connect to mysql $conn = @mysql_connect( "myserver", "username", "password" ) or die( "Could not connect" ); #select database $rs = @mysql_select_db( "removalspacelogin", $conn ) or die ( "Could not select db" ); #create query $sql="select * from users where username = '$username' and upassword = '$password'"; #execute query $rs = mysql_query( $sql, $conn ) or die( mysql_error() ); #get number of rows that match username and password $num = mysql_numrows( $rs ); #if there is a match the log-in is done if( $num != 0 ) { header( "Location:login_success.php" ); } else #or return to: login.php { header( "Location:$referer" ); exit(); } } ?> <?php ini_set("display_errors","ON"); ?> <html> <head><title>Check-login</title></head> <body> <?php echo( $msg ); ?> </body> </html> Register users script: Code: [Select] <?PHP $user_name = "removalspace"; $password = "123"; $database = "removalspacelogin"; $server = "removalspacecom.ipagemysql.com"; $db_handle = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $db_handle); if ($db_found) { $SQL = "INSERT INTO users (username, upassword, email) VALUES ('" .$username. "', '" .$upassword. "', '" .$email. "')"; $result = mysql_query($SQL); header( 'Location: http://www.removalspace.com/index.php' ); exit(); } else { print "Database NOT Found "; mysql_close($db_handle); } ?> I'm an experienced programmer with several languages (COBOL, REXX, Java, Javascript, etc.) under my belt over many years but I know very little about PHP. I dabbled in it a bit a year or two back and got it to do what I wanted to do but I'm not sure how to do what I currently need.
A bit of context: I have designed a website where the user can choose between various page styles; each style invokes different CSS to give them a different visual experience of the site. They can select the style they want via View/Style in Internet Exploder and View/Page Style in Firefox but that preference is only remembered as long as they're on that page. As soon as they move to another page on the site, they have to choose the alternate style again or live with the default style. I want them to be able to select that preference ONCE, the store it in a cookie specifically for them, then keep using that style every time they visit the site. (Naturally, I want them to be able to change the style and then remember the new style as well.) There will be many visitors and each will want that same capability. I currently don't require any login to the site. (I'm toying with password-protecting some of the site down the road but that's not a factor in this page style thing that I'm talking about here.
Now, my questions.
I've found a variety of short articles describing how to use setCookie() to create the cookie (and delete it later) and getCookie() or the newer echo $_COOKIE["name"] to determine the cookie value. However, I am NOT seeing anything that says where I put the code to set and/or read the code within my web pages. I would really appreciate an explanation of that! I expect to write short fragments of php code to do those jobs which I will then imbed within my web pages via SSI (Server Side Includes). Is that reasonable or is there a better way?
Also:
1. When the user clicks on the desired Style in the View/Style menu, how do I detect what choice they made?
2. Should the name of the cookie be something like "pageStyle" or should it be something that uniquely identifies the user so that the right cookie is retrieved when the come back? If the latter, what value should I use and how should I obtain it? I assume that something unique about the user obtained from their headers is far better than displaying a dialog and asking them for some unique identifier.
I'd rather stay away from Javascript in any of this functionality since I can't rely on it being turned on.
One quick aside while I'm here. Does anyone know how to change the page style if visitors use Google Chrome or Opera? I can't even FIND options for changing the page style in my copies of those two browsers and both are current versions. Do they not support differing page styles at all or do they just hide the technique to choose the style very effectively?
--
Henry
Im trying to make my form remember the last input made by user, so I wrote the following and I get a syntax error on the input line. The error is: Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\ezsynonym\index.php on line 90 Code: [Select] <form id="searchform" method="post" action="index.php"> <input id="input" name="synonymsearch" type="text" value="<?php echo $_POST['synonymsearch']; ?>" maxlength="400px" /> </form> TIA Hello everyone.
I have a fully working form that gets data from a user (with $_post array) , and stores it in a database (mysql).
After successfulIy filling the form, I refer him to the "dashboard" page. In this page, i am having trouble to get his details from the database. How should I recognize him as the user that just registered?
should I use a $_post? or maybe a session? could you please give me a clue how to solve this?
How do I get the current logged in username or id using the Twitter API? How can i save array from inputs, witch is $igraci and it have 5 values, to mysql base, i tryed but in mysql it shows Array. This is form: <form action="dodaj_klan.php" method="post" > <p> <label>Naziv klana:</label> <input name="naziv" type="text" size="20%" /> <label>Web Sajt:</label> <input name="website" type="text" size="20%" /> <label>E-Mail:</label> <input name="email" type="text" size="20%" /> <br /><br /> <label>Igraci klana(5):</label> <input name="lider" type="radio" value="1" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="2" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="3" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="4" /> <input name="igraci[]" type="text" size="20%" /><br /> <input name="lider" type="radio" value="5" /> <input name="igraci[]" type="text" size="20%" /> <label><i>(obelezi lidera klana)</i></label> <br /><br /> <input class="button" type="submit" name="submit" value="Dodaj" /> </p> </form> Now i wonna save this igraci[] array in mysql, i tried like this but it doesn't work: $igraci = $_POST['igraci']; $query = "INSERT INTO klanovi (naziv, website, email, igraci) VALUES ('{$naziv}', '{$website}', '{$email}', '{$igraci}')"; $result = mysql_query($query, $connection); How can i do this? Thanks.. Hi I am using php5 and Mysql I want to generate user id like phone format (Ex: xxx-xxx-xxxx). and check that user id exists in database or not. if yes, i have to generate new user id etc. If that user id not in database, i have to insert user id for new user. Means i have generate unique user id for login purpose. Generating user id is working fine. availability of user id also ok for me by using mysql_num_rows() function. But if user id i exists in database, how can i generate new user id. All this will be done in single instance (When user click on submit button from registration form). Here is my script. Code: [Select] <?php function UserID() { $starting_digit=5; $first_part1=rand(0,99); if(strlen($first_part1)==1) { $first_part="0".$first_part1; } else { $first_part=$first_part1; } $second_part1=rand(1,999); if(strlen($second_part1)==1) { $second_part="00".$second_part1; } elseif(strlen($second_part1)==2) { $second_part="0".$second_part1; } else { $second_part=$second_part1; } $third_part1=rand(1,9999); if(strlen($third_part1)==1) { $third_part="000".$third_part1; } elseif(strlen($third_part1)==2) { $third_part="00".$third_part1; } elseif(strlen($third_part1)==3) { $third_part="0".$third_part1; } else { $third_part=$third_part1; } $userid=$starting_digit.$first_part."-".$second_part."-".$third_part; return $userid; } $random_userid=UserID(); $sql=mysql_query("select * from users where user_id='".$random_userid."'"); if(mysql_num_rows($sql)==0) { $insert=mysql_query("insert into users (user_id) values ('".$random_userid."')"); } ?> If user id not exists in database, that user id inserting in database successfully. If not, empty value is inserting, not generating new user id. Can any one help me out please. So if I change the user his data to banned or something he don't change Here is the part of the code: Code: [Select] $userInfo = $db->getuserInfo($_GET['id']); if ($userInfo) { echo '<form action="'.coreLink('admin', 'users').'" method="post"><input type="hidden" name="userID" value="'.$userInfo['ID'].'"><ul class="inputs"> <li> <div style="float:right"><input type="text" name="username" class="input" value="'.$userInfo['username'].'"></div> '.__('Username').'<br /><small>'.__('Nickname of the user').'</small> </li> <li> <div style="float:right"><input type="text" name="pass" class="input" value=""></div> '.__('Change password').'<br /><small>'.__("Fill this input if you want to change it's password").'</small> </li> <li> <div style="float:right"><input type="text" name="email" class="input" value="'.$userInfo['email'].'"></div> '.__('Email').'<br /><small>'.__('Email of the user').'</small> </li> <li> <div style="float:right"><input type="text" name="api" class="input" value="'.$userInfo['api'].'"></div> '.__('API Code').'<br /><small>'.__('Used to access through the API').'</small> </li> <li> <div style="float:right"><select class="input" name="status" style="width:311px">'; foreach (array('ok'=>__('Active'), 'nc'=>__('Not confirmed'), 'banned'=>__('Banned')) as $key=>$long) { echo '<option value="'.$key.'"'; if ($key == $userInfo['status']) echo ' selected'; echo '>'.$long.'</option>'; } echo '</select> </div> '.__('Status').'<br /><small>'.__('It can be active, banned..').'</small> </li> <li> <fieldset id="shorters" style="border: 1px solid #ddd;-moz-border-radius:2px;-webkit-border-radius:2px"><legend>'.__('Profile').'</legend><br /><div style="padding-left:40px;margin-bottom:20px;width:110px">'; $avatar = getAvatar($userInfo['ID'], '48'); if (!$userInfo['gravatar'] && ($avatar != $jk->base."static/img/avatar/default_note.png")) echo '<div style="float:right;font-size:.8em"><input type="checkbox" name="avatar"> <img src="'.$jk->base.'static/img/trash.gif"></div>'; echo '<img src="'.getAvatar($userInfo['ID'], '48').'" style="border: 1px solid #ddd"></div> <ul> <li> <div style="float:right"><input type="text" name="profile_name" class="input" value="'.$userInfo['realname'].'"></div> '.__('Name').'<br /><small>'.__('Real name of the user').'</small> </li> <li> <div style="float:right"><input type="text" name="profile_web" class="input" value="'.$userInfo['profile']['url'].'"></div> '.__('Website').'<br /><small>'.__('Website of the user').'</small> </li> <li> <div style="float:right"><input type="text" name="profile_location" class="input" value="'.$userInfo['location'].'"></div> '.__('Location').'<br /><small>'.__('Location of the user').'</small> </li> <li> <div style="float:right"><input type="text" name="profile_bio" class="input" value="'.$userInfo['profile']['bio'].'"></div> '.__('Bio').'<br /><small>'.__('Description of the user').'</small> </li> </ul></fieldset> </li> <li> <div style="float:right"><select class="input" name="language" style="width:311px">'; foreach (return_languages() as $short=>$lang) { echo '<option value="'.$short.'"'; if ($short == $userInfo['language']) echo ' selected'; echo '>'.$lang.'</option>'; } echo '</select> </div> '.__('Language').'<br /><small>'.__('Language of the user').'</small> </li> <li> <div style="float:right"><select class="input" name="theme" style="width:311px">'; foreach ($jk->allowed_themes as $theme) { echo '<option value="'.$theme.'"'; if ($theme == $userInfo['theme']) echo ' selected'; echo '>'.$theme.'</option>'; } echo '</select> </div> '.__('Theme').'<br /><small>'.__('Skin of Jisko').'</small> </li> <li> <div style="float:right"><input type="text" name="invitations" class="input" value="'.$userInfo['invitations'].'"></div> '.__('Number of invitations').'<br /><small>'.__('Number of invitations that the user has').'</small> </li> <li> <div style="float:right"><input type="text" name="openid" class="input" value="'.$userInfo['openid'].'"></div> '.__('OpenID').'<br /><small>'.__('Used to access Jisko trough an OpenID account').'</small> </li> <li> <div style="float:right"><input type="text" name="facebook" class="input" value="'.$userInfo['facebook'].'"></div> '.__('Facebook ID').'<br /><small>'.__('Used to access Jisko trough a Facebook account').'</small> </li> </ul> <br /><input type="submit" value="'.__('Save').'"><br /><br /></form>'; } else header('Location: '.coreLink('admin', 'users')); } I have a page site.com/register/signup.php I want to echo out the current directory: register. When I use getcwd() I get: /home/user1234/public_html/register instead of: register or if I am inside this directory site.com/register/image/ then I want to echo out: register/image I have also tried the server[self] command but that too gives you the full directory. Does anyone know how to echo out only the current directory starting AFTER the public_html? I am trying to populate a custom field called "Customer Type" current user role. The custom field is displayed on my checkout page. I tried the below in my functions.php of my child theme and thought it would work but it does nothing. Can anyone tell me what I might be doing wrong?
$user = wp_get_current_user(); $fields['customertype'] = $user;
return $fields; add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' ); Edited April 11 by JayXshow list of files uploaded by current session user to the database. I want to show different users when they log in to the website...they can see a list of old files that they have uploaded. can anyone tell me the code/script to this.....please, ty Hello all, I need to make a rotating banner system for a client. They want about 5 banners to rotate with Javascript. However they want the visitor to sort of step through each one on each refresh. So if I have 5 banners and I go to the site, I see banner 1 and then the javascript rotates through each one. When I go back to the site, they want it to start with banner 2 and then the javascript will rotate through each one. Then on and on. Go back and see banner 3, then step through again. I have a nice Javascript image slider I'd like to use. It basically just requires the images in order. Like: <img src="1.jpg"/> <img src="2.jpg"/> <img src="3.jpg"/> <img src="4.jpg"/> <img src="5.jpg"/> I've thought about it and I think the best way to do this would just be using PHP and a cookie. So when the viewer first goes to the site, PHP checks for the cookie. If it exists, it pulls the starting image #. If it doesn't, it starts at 0 and sets the cookie. As for the images part, I figured an associate array would work well. The key would be what the cookie logic is based on, and then the image HTML would be in the array. So you visit the site without a cookie it would be... 0 => <img src="1.jpg"/> 1 => <img src="2.jpg"/> 2 => <img src="3.jpg"/> 3 => <img src="4.jpg"/> 4 => <img src="5.jpg"/> Then when you go back to the site, I need the array to be re-ordered like this: 0 => <img src="2.jpg"/> 1 => <img src="3.jpg"/> 2 => <img src="4.jpg"/> 3 => <img src="5.jpg"/> 4 => <img src="1.jpg"/> What's the best way to go about this array logic? My experience with manipulating data inside arrays is rather limited. Basically I need it to say "Start with key # and then reorder array based on that" good day here! I'm new in php, and i want to save diff array at the same time. is it possible? if (isset($_POST['submit'])) { $user_Id = $_POST['user_Id']; $se_Id = $_POST['se_Id']; $status = $_POST['status']; foreach ($_POST['user_Id'] as $user_Id) <-- this is working, but I want to include the other array { mysql_query("INSERT tbl_data (user_Id,se_Id,status) VALUES ('$user_Id','$se_Id,'$status'')") or die(mysql_error()); } } help me guys. $_SESSION[$c] is an array in a loop. How do i do an if statement that gets the current array key? Code: [Select] if($_SESSION[$c][]==$_SESSION[$c][1]) { } i tried the above but i get this error "Fatal error: Cannot use [] for reading in..." Hey, my code doesnt work and I dont know why: $update_weed_world_data_sql = "SELECT weed_factor, weed_basis FROM worlds"; $update_weed_world_data_res = mysql_query($update_weed_world_data_sql) OR DIE (mysql_error()); while($update_weed_world_data_while = mysql_fetch_assoc($update_weed_world_data_res)){ $update_weed_world_basis[''.$update_weed_world_data_while['id'].''] = $update_weed_world_data_while['weed_basis']; $update_weed_world_factor[''.$update_weed_world_data_while['id'].''] = $update_weed_world_data_while['weed_factor']; } I want to save some rows in variables identifiable by the value in the array. As example. When I want to echo $update_weed_world_factor['1'] - I want to get the row with ID 1 and the column "world_factor". I've used this code long time ago - but know something was going wrong. Thanks for helping :-) Hi guys, I'm trying to sort an array. The array has information about listings. ID, name, info etc. An unrelated set of data also contains the listing ID's and I have made a function to sort this array and show an new order for the ID's. I now need to sort the original array by this new ID order... This snippet shows that the original listings array can be sorted by link_id: sort($this->links, $this->link->link_id); //This works. It sorts links by link ID! In a foeach loop I have created this: $sleeps_array[] = array("ID" => $link->link_id, "Sleeps" => $sleeps); // Makes an array of arrays of a custom field with listing ID. It makes this: array ( 0 => array ( 'ID' => '9', 'Sleeps' => '2', ), 1 => array ( 'ID' => '3', 'Sleeps' => '4', ), 2 => array ( 'ID' => '6', 'Sleeps' => '6', ), ) I can then sort this array of arrays with this function: function compare_sleeps($a, $b){ return strnatcmp($a['Sleeps'], $b['Sleeps']); } # sort alphabetically by name usort($sleeps_array, 'compare_sleeps'); It then gives me a sorted array according to "Sleeps" (as in, the number of people a property can cater for). It has the correct order of ID to then output but how do I organise the original array by ID with this new array? Sounds complicated, I hope you got that! Cheers, RJP1 Hi, I'm trying to save data from an array to database but something is wrong. This is from i send data, as you see im using arrays [] because the user have the posibility to clone the row <div class="box-body"> <div class="form-group"> <label class="col-sm-2">Student</label> <div class="col-sm-10"> <table id="table" border="1" bordercolor="#00acc1"> <thead> <tr> <th><p>Name</p></th> <th><p>Mode</p></th> <th><p>Sport</p></th> <th> </th> </tr> </thead> <tbody> <tr> <td><select style="width:325px" name="Name[]" class="form-control"> <option value="">Select</option> <?php $search = "SELECT * FROM prof"; $data = $connect->prepare($search); $data->execute(); while($re=$data -> fetch(PDO::FETCH_ASSOC)){ echo "<option value = '".$re['id']."'>"; echo $re['n_prof'].' '.$re['ap_prof'];} ?> </select> </td> <td> <select class="form-control" name="mode[]" style="width:150px" /> <option value="">Select</option> <option value="Administrator">Administrator</option> <option value="Scholar">Scholar</option> <option value="External student">External student</option> <option value="Thesis">Thesis</option> <option value="Voluntary">Voluntary</option> </select> </td> <td> <select class="form-control" name="Sport[]" style="width:150px" /> <option value="">Select</option> <option value="Football">Football</option> <option value="Baseball">Baseball</option> <option value="Swimming">Swimming</option> <option value="Horse riding">Horse riding</option> <option value="basketball">basketball</option> </select> </td> <td class="Delete">Delete</td> </tr> </tbody> </table> <input type="button" id="add" value="+ add student" class="btn btn-primary"/> </div> </div> </div> my db.table is like this id | name | mode | sport | idstudent_fk And Im using an algorithm to read the array and every time can save every row, but im usind PDO and im having problems try{ here i insert data in a table here i got the last id from that table then ... if($_POST['name']!="" and $_POST['mode']!="" and $_POST['sport']!=""){ if(is_array($_POST['name'])){ while(list($key, $name) = each($_POST['name']) and list($val,$mode) = each($_POST['mode']) and list($id, $sport) = each($_POST['sport'])){ $sql = "INSERT INTO sports(id_studentfk, mode_stu, sport, id_projfk) values(:value, :mode, :sport, :lastid)"; $statement = $connect ->prepare($sql); $statement -> bindParam(':name', $name, PDO::PARAM_INT); $statement -> bindParam(':mode', $mode, PDO::PARAM_STR); $statement -> bindParam(':sport', $sport, PDO::PARAM_STR); $statement -> bindParam(':lastid', $lastid, PDO::PARAM_INT); $pdoExec = $statement -> execute(); } }//end if array }//end if post } catch (PDOException $e) { print 'ERROR: '. $e->getMessage(); print '<br/>Data Not Inserted'; } the idea is creating a loop to read the array and every time can insert data. Im using PDO this time is not working, im geeting this error
ERROR: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined I hope someone can help me to solve this
|