php javascript select - php

i need a help
in php i have a select box like this
<?php
$perpage = 5;
$total = 128;
$num = ceil( $total / $perpage );
$i = 1;
echo "<FORM NAME=\"form1\">";
echo "Select <SELECT NAME=\"select\" onChange=\"goto(this.form)\" SIZE=\"1\" >";
echo "<OPTION VALUE=\"\">----Select Page----";
for($i = 1; $i <= $num; $i++)
{
$sel = $i;
$goto = ($i - 1) * $perpage;
if($goto == 0) { $goto = ''; }
echo "<OPTION VALUE=\"http://localhost/CI_doctrine/blog/filmnews/" . $goto . "\">" . $i . "";
}
echo "</SELECT> Page";
echo "</FORM>";
?>
javascript code is here
function goto(form) {
var index = form.select.selectedIndex;
if (form.select.options[index].value != "0") {
window.location = form.select.options[index].value;
form.select.selected = form.select.options[index].value;
}
}
the code is working fine but i want to change the selected option to set the selected number after the page redirection but here iam getting the "select page" as the selected option
any help appreciated.
thank you from your friend.

Once you redirect, that page is unloaded and a new page loaded (even if it has the same items). When the new page loads, you want to do:
window.onload = function() {
var i, options = form.select.options, curUrl = window.location.href;
for (i = 0; i < options.length; i++) {
if (options[i].value == curUrl) {
form.select.selectedIndex= i;
break;
}
}
}
This will select the current URL. Make sure the URLs in the select options are full URLs (including http://). Also, window.onload is the DOM1 way. You should probably use a real library to deal with this.
Alternatively, you can also select the right input in PHP using the same basic approach.

Related

php Variable name must change in for loop

On my form I've got a select from a table. I've put it in a for loop so that each time the name of the select changes to option0, option1, etc. Then on the action page of the form I need to get the info for each option selected, eg $_POST['option0'], $_POST['option1'], etc to input it into a table. I can't get the action page to work, what am I doing wrong?
PAGE1
for ($x = 0; $x <= 14; $x++) {
$get2a = mysqli_query($con,"SELECT * FROM table");
$opt = "<select name='interviewer". $x . "'>";
while($row2a = mysqli_fetch_array($get2a)) {
$intvID = $row2a['intvID'];
$opt .= "<option value = '";
$opt .= $row2a['intvID'];
$opt .= "'>";
$opt .= $row2a['intvID'];
$opt .= "</option>";
}
PAGE 2
for ($y = 0; $y <= 14; $y++) {
echo $_POST['interviewer . $y . '];
}
You are calling your selectable options sets $opt = "<select name='option". $x . "'>";
So Page2 should be looking for things names 'option0' ... 'option13'` like this
for ($y = 0; $y <= 14; $y++) {
echo $_POST['option' . $y]; <-- notice slight syntax change also
}
Have you tried printing the post array? Use print_r:
print_r($_POST);
I don't believe the post has something that is named 'interviewer .... something.
besides, if you wish to mix text and variables, use
$_POST["interviewer {$x}"]
remove the single quotes and the dots. Single quotes do not evaluate variables:
$_POST['interviewer . $y . ']; -> wrong

How to retain selected item of ddl in php?

I have three drop down lists and I need to retain selected item in ddl after form is submitted
e.g. When I click 09 in ddl and submit form then it should display 09 in ddl. Pasting my code for your reference.
echo"<option> Select Day</option>";
for ($i = 1; $i <= 31; $i++)
{
if($i<10){
echo "<option>".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
}
else{
echo "<option>".$i."</option>";
}
}
?>
I don't know what your DDL name attribute is, so I'll pretend
<select name="ddl"­>
Here's what you code should look like:
echo"<option> Select Day</option>";
for ($i = 1; $i <= 31; $i++)
{
if(isset($_POST['ddl']) && $_POST['ddl'] == $i)
$select = " checked='checked'";
else
$select = "";
if($i<10){
echo "<option " . $select . ">".str_pad($i,2,"0",STR_PAD_LEFT)."</option>";
}
else{
echo "<option " . $select . ">".$i."</option>";
}
}
?>
This is a base example, you should take measure to protect your code against injection.

How can I "limit" the page numbers of a forum/blog in PHP

I'm trying to fix something with which I'm not familiar with and don't know how to proceed. The forum on which I'm working is suppose to show under "TOP 50" only the most commented topics (2 pages by 25 topics) but it shows all topics (by 25) without any limitation of the pages. I need only the first 2 pages - but don't know how to get rid of the others?
I'm even not sure that the below code is the responsible one but please have a look and give me a hint if you see any solution.
This is the code:
{
public function __construct()
{
parent::__construct();
}
public function get_forum()
{
if ($_GET['l'] && ($_GET['l'] == 'leng' || $_GET['l'] == 'lrus' || $_GET['l'] == 'lde' || $_GET['l'] == 'ltr'))
$l = substr($_GET['l'], 1);
else
$l = 'eng';
(isset($_GET['num'])) ? $page = intval($_GET['num']) : $page = 1;
$id_user = intval($_SESSION['user_id']);
$lang = language::getLang();
if ($_GET['el']) {
switch ($_GET['el']) {
case 'categories':
return $this->getCategories($l);
break;
case 'top':
$top_lang = $_GET['ln'];
$c = $this->db->selectAssoc($this->db->Select('*', 'forum_categories ,forum_thems', "`forum_categories`.`lang` = '" . $l
. "' AND `forum_thems`.`id_categories` = `forum_categories`.`id`"));
$total_pages = count($c) / 25;
$p = "<div class=\"pageCounter_box\">Pages:";
if (empty($_GET['p'])) {
$_GET['p'] = 1;
}
for($i=1; $i<$total_pages+1; $i++){
if ($i == $_GET['p']) {
$class = 'class="active_page"';
}
$p .= "<a href=\"$top_lang/smoke/{$_GET['l']}/top?p=$i\" $class>$i</a>";
}
$p .= "</div>";
return $this->getTop($l) . $p;
break;
I think you could do a check in there of If ($total_pages > 2) { $total_pages = 2};
$c = $this->db->selectAssoc(
$this->db->Select('*', 'forum_categories ,forum_thems', "`forum_categories`.
`lang` = '" . $l. "' AND `forum_thems`.
`id_categories` = `forum_categories`.`id`"));
$total_pages = count($c) / 25;
if ($total_pages >2) { //limit to two pages
$total_pages = 2;
}
$p = "<div class=\"pageCounter_box\">Pages:";
if (empty($_GET['p'])) {
$_GET['p'] = 1;
}
"thanks a lot - great help! Do you further see why both pages might show active (page counter shows both active) when showing page 1? Page 2 is fine, there only Page 2 shows active..."
The $class variable is staying set, you need to have an else that sets the class to an empty string
for($i=1; $i<$total_pages+1; $i++){
if ($i == $_GET['p']) {
$class = 'class="active_page"';
} else {
$class = '';
}
$p .= "<a href=\"$top_lang/smoke/{$_GET['l']}/top?p=$i\" $class>$i</a>";
}

PHP from external anchor to internal

I have modified my project into a one page website type.
Part of the code is this:
for ($i = 1; $i <= $total_pages; $i++)
{
echo ''.$i.'';
}
I need that code above to work with an internal link for example:
From here:
echo ''.$i.'';
TO
echo ''.$i.'';
Here is the full source code:
$per_page = 6;
$result = mysql_query("SELECT * FROM mytable ORDER BY date DESC");
$total_results = mysql_num_rows($result);
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']))
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
// error - show first set of results
$start = 0;
$end = $per_page;
}
}
else
{
// if page isn't set, show first set of results
$start = 0;
$end = $per_page;
}
// display pagination
echo '<strong>View Page:</strong> ';
echo '<div data-role="controlgroup" data-type="horizontal">';
for ($i = 1; $i <= $total_pages; $i++)
{
echo ''.$i.'';
//echo ''.$i.'';
}
echo '</div>';
I need to change this:
echo ''.$i.'';
so it's internal.
Short answer: This can't be done in PHP alone. You will need send AJAX requests to a PHP script that grabs the data.
Basically, this will involve creating some javascript that listens for any changes to the hash (that's the part after the #);
(function(window) {
var hash = window.location.hash;
setTimeout(function() {
if(window.location.hash !== hash) {
hash = window.location.hash;
//Grab and update page with ajax...
}
setTimeout(arguments.callee, 50);
}, 50);
})(window);
For AJAX basics, check W3Schools. And please, please respect the back button.
Without reloading the page:
foo
If you modify the query string in the link, the page will reload and jump to the anchor:
foo
Or with JQuery:
<script type="text/javascript">
$(document).ready(function(){
window.location.hash = 'my-anchor';
});
</script>

How can I stop certain part of my php script from executing? (to make my page load faster)

<?php
$rows_per_page = 2;
$cols_per_page = 2;
$image_href = '<a href=/';
$image_links = array('file1/page1>', 'file2/page2>', 'file3/page3>',
'file4/page4>', 'file5/page5>', 'file6/page6>', 'file6/page6>',
'file/page7>', 'file/page8>', 'subfile/page9>');
$img_srcs = '<img src="https://s3.amazonaws.com/imagetitle/';
$images = array();
for($i = 1; $i < 10; $i++)
{
$images[$i] = $i;
}
$image_ending = '.png" height="200" width="200" /></a>';
$image_descriptions = array('<br />something', '<br />description',
'<br />arbitrary', 'random', '<br />you', '<br />get', '<br />the',
'<br />idea', '<br />itsdescriptions');
$total_images = count($images);
$images_per_page = $rows_per_page * $cols_per_page;
$total_images = count($images);
$total_pages = ceil($total_images / $images_per_page);
$current_page = (int) $_GET['page'];
if($current_page<1 || $current_page>$total_pages)
{
$current_page = 1;
}
//Get records for the current page
$page_image_links = array_splice($image_links, ($current_page-1)*$images_per_page, $images_per_page);
$page_images = array_splice($images, ($current_page-1)*$images_per_page, $images_per_page);
$page_image_descriptions = array_splice($image_descriptions, ($current_page-1)*$images_per_page, $images_per_page);
$slots = "<table border=\"1\">";
for($row=0; $row<$rows_per_page; $row++)
{
$slots .= "<tr>";
for($col=0; $col<$cols_per_page; $col++)
{
$imgIdx = ($row * $rows_per_page) + $col;
$img = (isset($page_images[$imgIdx])) ? "{$image_href}{$page_image_links[$imgIdx]}{$img_srcs}{$page_images[$imgIdx]}{$image_ending}{$page_image_descriptions[$imgIdx]}" : ' ';
$slots .= "<td>$img</td>";
}
$slots .= "</tr>";
}
$slots .= "</table>";
//Create pagination links
$first = "First";
$prev = "Prev";
$next = "Next";
$last = "Last";
if($current_page>1)
{
$prevPage = $current_page - 1;
$first = "First";
$prev = "Prev";
}
if($current_page<$total_pages)
{
$nextPage = $current_page + 1;
$next = "Next";
$last = "Last";
}
?>
<html>
<title></title>
<body>
<h2>Here are the records for page <?php echo $current_page; ?></h2>
<ul>
<?php echo $slots; ?>
</ul>
Page <?php echo $current_page; ?> of <?php echo $total_pages; ?>
<br />
<?php echo "{$first} | {$prev} | {$next} | {$last}"; ?>
</body>
</html>
So basically this code above makes it easy to put up images and their links. For now, I don't have that many images, so the page runs super fast. However, I'm thinking in the long run. If I have say, 10,000 images, it's gonna take a long time to process each paginated page, as I would have $image_links of file1/page1 up to file10000/page10000, and 10000 descriptions! Is there a way to stop the web browser from reading, or skip, certain parts of the script (the $image_links and $image_descriptions in my case)? That way, it won't need to read through all 10000 $image_links and $image_descriptions.
Thanks!
I'm going to suggest another approach. Using arrays for this is not ideal; it really feels like it should be a database driven app.
If you decide to go for that approach, pagination becomes much easier. You can just ask your database engine for the corresponding records. For example, in MySQL, you'd ask for the data something like this:
select * from tableA order by id limit 10 offset 20;
This would return id's 21 thru 30.
... and, would scale much better. Of course, if you've never done any work with databases, there is quite a learning curve.
If you want to end execution at a certain place, just call exit;
Try using the onload function on your < body >'s html, so it will load the page at once

Categories