-
Posts
16,734 -
Joined
-
Last visited
-
Days Won
9
Everything posted by PFMaBiSmAd
-
Store multiple selection from pagination info into single array
PFMaBiSmAd replied to ktsirig's topic in PHP Coding Help
Only if you don't clear the post data. See following example, based on the phpfreaks pagination tutorial - <?php session_start(); // database connection info $conn = mysql_connect('localhost','dbusername','dbpassword') or trigger_error("SQL", E_USER_ERROR); mysql_select_db('dbname',$conn) or trigger_error("SQL", E_USER_ERROR); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM numbers"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); list($numrows) = mysql_fetch_row($result); // number of rows to show per page $rowsperpage = 25; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $newpage = (int)$_GET['currentpage']; } else { // default page num $newpage = 1; } // end if // modify the requested page based on any pagination form buttons, set/clear any remembered session checkbox data $page = isset($_POST['page']) ? trim($_POST['page']) : false; if($page){ // values of <<, <, x, >, or >> switch($page){ case '<<': $newpage = 1; break; case '<': $newpage--; break; case '>': $newpage++; break; case '>>': $newpage = $totalpages; break; default: // not one of the symbols, should be a number $page = intval($page); if($page > 0){ $newpage = $page; } break; } // set or clear the state of the check boxes in $_SESSION['choice'] // $_SESSION['ids'] = array of ids on the page that submitted foreach($_SESSION['ids'] as $key){ if(isset($_POST['choice'][$key])){ $_SESSION['choice'][$key] = 1; } elseif (isset($_SESSION['choice'][$key])) { unset($_SESSION['choice'][$key]); } } header("location: {$_SERVER['SCRIPT_NAME']}?currentpage=$newpage"); // clear post data exit; } // if current page is greater than total pages... if ($newpage > $totalpages) { // set current page to last page $newpage = $totalpages; } // end if // if current page is less than first page... if ($newpage < 1) { // set current page to first page $newpage = 1; } // end if /****** build the pagination links ******/ // I moved this up in the code so that the links are built first so they can be output anywhere in the content // range of num links to show $range = 3; $links = ''; // if not on page 1, don't show back links if ($newpage > 1) { // show << link to go back to page 1 $links .= "<input type='submit' name='page' value='<<'>"; // show < link to go back 1 page $links .= "<input type='submit' name='page' value='<'>"; } // end if // loop to show links to range of pages around current page for ($x = ($newpage - $range); $x < (($newpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $newpage) { // 'highlight' it but don't make a link $links .= " [<b>$x</b>] "; // if not current page... } else { $links .= "<input type='submit' name='page' value='$x'>"; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($newpage != $totalpages) { // echo forward link for next page $links .= "<input type='submit' name='page' value='>'>"; // echo forward link for lastpage $links .= "<input type='submit' name='page' value='>>'>"; } // end if /****** end build pagination links ******/ /****** get the actual database content for the requested page ******/ // the offset of the list, based on current page $offset = ($newpage - 1) * $rowsperpage; // get the info from the db $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage"; $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR); echo "<form action='?currentpage=$newpage' method='post'>"; echo $links . '<br />'; // show the pagination buttons // while there are rows to be fetched... $ids_this_page = array(); // a list of the checkbox id's on this page while ($list = mysql_fetch_assoc($result)) { // echo data $checked = isset($_SESSION['choice'][$list['id']]) ? " checked='checked'" : ''; echo "{$list['id']}:{$list['number']} <input type='checkbox' name='choice[{$list['id']}]' value='1'$checked><br />"; $ids_this_page[] = $list['id']; } // end while $_SESSION['ids'] = $ids_this_page; echo "<input type='submit' name='submit' value='Retrieve'>"; echo "<br />"; echo $links . '<br />'; // show the pagination buttons echo "</form>"; ?> -
Store multiple selection from pagination info into single array
PFMaBiSmAd replied to ktsirig's topic in PHP Coding Help
The only pure html/php way would be if the pagination 'links' are actually form submit buttons and you are actually submitting the check box data on one page to another page when you click on a pagination button. -
It would appear that your table already has an auto-increment column. All you would need to do is use that existing column as the id.
-
Actually, the forum's code tag is undesirable as it adds a new-line character to the html source after every \t, \r and/or \n it finds, when the source code uses a different line ending than the one that the programmers were using when they wrote and tested the code for the code tag. When you copy paste the code to help with it, you get hundreds of extra new-lines in it and it is a mess. The tag should be used.
-
You need to keep track of the id of the last item you have displayed. Then, you simply query for items having an id greater than that value. You would either store the last id in a session variable or carry it on the of the url as a get parameter.
-
As someone suggested in a previous thread, your product_id should be the auto-increment identifier from your database table, not a text string.
-
mysql_fetch_assoc only returns an associative array. Perhaps you meant to use mysql_fetch_row or mysql_fetch_array ?
-
To get a specific row within a group, see this link - http://dev.mysql.com/doc/refman/5.5/en/example-maximum-column-group-row.html Otherwise, you get the data from the first row encountered in the table.
-
$result = mysql_query($query); // execute query once if (!$result) { die('Invalid query: ' . mysql_error()); } // assume the query matched at least one row $row = mysql_fetch_array($result); // get data for the ... vs. Active Teams output $wins=0; $losses=0; $draws=0; $last_target = false; echo '<h2>' . $row['selected_team'] . ' vs. Active Teams</h2>'; mysql_data_seek($result,0); while ($row = mysql_fetch_assoc($result))
-
As already stated, you are fetching the first row of data from the result set before the start of your while(){} loop. The first pass through the while(){} loop then fetches the 2nd row of he result set. You should also only execute the query once. To reset the result pointer back to the first row in the result set after you get the data to produce the ... vs. Active Teams output, use mysql_data_seek($result,0);
-
Your code is fetching the first row before the start of your while(){} loop, so of course you are missing one row of the result set. You are also executing the query twice, why are you doing that? $result = mysql_query($query); // execute query once $row = mysql_fetch_array($result); // fetch the first row from the result set and advance the result pointer to the next row $result1 = mysql_query($query); // execute the query again $row1 = mysql_fetch_array($result1); // fetch the first row from the result set and advance the result pointer to the next row
-
I don't think you found a $PHP_SELF on that site, but yes, that would be an old and out of date feature. The setting that causes $PHP_SELF to exist was depreciated and turned off by default in php4.2 in April of 2002, throws a depreciated error when turned on in php5.3 (which is when the depreciated error type was added), and has been completely removed in php5.4.
-
What makes you think it is outdated? I just paged through to at least the 'switch' statement page and the only thing mentioned up to that point that should not have been is that there is a short opening php tag (which did state that it must be enabled in order to be used.) Our advice is to never use the short opening php tag. edit; I'm up to the magic_quotes page and the only thing so far is it mentioning php6. That information should state it apples to php5.4 edit2: It should probably mention testing for upload errors on the upload page. Actually, any basic programming tutorial should mention, up front, the need to test for possible errors at each step in the process so that you don't blindly attempt to use non-existent data and trigger follow-on errors.
-
The only thing I asked you to do is to navigate to a different sub-domain and recheck the value. Any chance you are using https on some of these sub-domains and not on others?
-
Does the PHPSESSID cookie domain setting have the leading dot as part of it? Is there more than one PHPSESSID cookie, for different variations of your domain, that are left over from previous testing? You may need to delete them all so that a new one that matches the current session.cookie settings will get created. When you log in under one sub-domain, look at the value (content) in the PHPSESSID cookie, then navigate to a different sub-domain and check the PHPSESSID cookie value (content) again. Are those two values the same?
-
Also, what does a phpinfo statement show for the session.cookie_path setting?
-
When you do log in, what does the PHPSESSID cookie have for a domain setting when you look at the cookie in your browser? Are you putting the ini_set() statements before the session_start() statement? Is the ini_set() statement enabled on your server? You should be able to put php settings into a local php.ini (when php is running as a CGI application) or into a .htaccess file (on Apache when php is running as an Apache Module.) What does a phpinfo statement show for the session.auto_start setting?
-
Use one variable value in another variable under same class
PFMaBiSmAd replied to ankur0101's topic in PHP Coding Help
The problem is that you cannot use variable values or even the dot concatenation operator in the declaration of your class properties. You can only assign fixed/constant values when you declare a class property. To do what you want, you will need to assign that string to the $add_form property at runtime using code. -
[Edit: longer version of what awjudd stated above...] Since you haven't provided any actual information about what wrong output you are getting and where and at what point it is happening and you don't seem to have the troubleshooting skills to debug the code yourself, yes that would be the only way. You started this thread suggesting the code cannot connect to the database, posted the two lines mysql_connect/mysql_select_db code, without stating what symptom you got that makes you believe the problem is because of the database connection. In reply #2, your statement - "I just cant see the products on the list if i click on it" is pointless (except it probably does indicate that the page is getting information out of the database to produce links for the products) because you haven't provided any information about what list you are talking about, what code and data produced that list, if the links in the list are correct, what exactly happened when you clicked on a link, what the code is that runs when a link is clicked, what output you did get, what was incorrect about that output, what the expected output was supposed to be ... You don't have enough experience with php to know how to set its error reporting/display errors settings to get php to help you. And when you did do this, you cannot determine the meaning of an undefined variable message. In reply #10 I suggested some specific things that you would need to look at to pin down where the problem is occurring at. Your reply was - "I cannot load products or users". That doesn't tell us anything useful, without the complete code and data that reproduces that symptom. In reply #13, you posted some code, but didn't state exactly what symptom you got that leads you to believe there is a problem with that code. You didn't state or show what output you got from that code, what was wrong with that output, and what the expected output should have been. The only thing that code showed is there is nothing in that piece of posted code that has anything to do with php4 vs php5 and that the person who originally wrote this code didn't know anything about securing web pages. You seem to think that there is an exact one to one relationship between each symptom and the problem causing that symptom or that someone can tell you exactly what your code is doing that is no longer working when the php version or more likely the php.ini settings changed, without seeing your code and at least an example of your data. Programming does not work that way. It's impossible to tell what is causing any one symptom without seeing the code and data that produces that symptom. In programming, due to its general purpose nature and multiple ways of accomplishing any task, there is not a one to one relationship between any symptom and what is causing it. Six different people could have written code that implements the same application and they could all be getting the same exact symptom, but the actual cause could be different in each case because of differences in server settings or in their actual code, data, and methodology used to implement the application. The only way anyone in a help forum can help you fix any one symptom or error is if you post all the relevant code and data needed to reproduce that symptom or error, state exactly what output, symptom, or error you got (and if you got a blank page, what does the 'view source' of that blank page show in your browser) , and usually state what the expected output should have been. Sorry for the above 'reading the riot act' to you, but we are not standing right next to you and only see the information you supply. You want a specific answer based on vague 'the code worked before, doesn't now' statements, but we cannot tell you anything specific without specifically having the relevant code and data.
-
Unfortunately, Apache on case-sensitive operating systems causes URL's to be case-sensitive. You need to use the exact same capitalization as your folders names in any link/url - Images I would recommend using all lower-case for folders and file names. P.S. You have options indexes on, which allows directory browsing (browse to any folder path.) You also don't have a default document in your root folder. You need a .htaccess file with Options -Indexes to prevent directory browsing.
-
That would be the correct syntax. What exact problem are you trying to solve?
-
There's nothing in the posted code that is php4/php5 version specific or even php.ini configuration specific. P.S. Your login check code is not secure. All a hacker needs to do is ignore the header() redirect and he can access the code on that page the same as if he was logged in. You need an exit; statement after the header() redirect to prevent the remainder of the code on that page from running while the browser requests the new page. Also, anyone can create and set a cookie to any value, so anyone could create a $_COOKIE['user'] and visit your site and appear to be logged in as any user.
-
You are getting a query was empty error because the variable you are putting into the mysql_query() statement doesn't exist. It isn't the variable name you are forming the query statement in.
-
If you only want the part of the string after the :, there's no point in worrying about what is before the :, unless you are specifically trying to find just that pattern among a bunch of content, but you would have probably shown us if that was the case. <?php $string = "London, May 1: Hi there"; list(,$string) = explode(':',$string); $string = str_replace(' ','%20',$string); echo $string;
-
What have you done to troubleshoot why you cannot see the products when you click on it? Is the link properly formed and has the correct information in it? What does the code at the target of that link expect for an input value? Is that input value present in the code when it runs? If the input value is present, what does the code do with that value? Is there matching data present in the database? You are the only one here who can troubleshoot what your code and data are doing on your server, to pin down exactly at what point your code and data are doing what you expect and exactly at what point they are not. I can guarantee that the problem lies somewhere between those two points. If all you have done is to try to run the code on a page and you cannot see the products on the list if you click on it, all you have done is narrowed down the problem to the code that page. You have to narrow down the problem to a specific statement or a specific variable or a specific value that is not doing what you expect.