Answer the question
In order to leave comments, you need to log in
Convert a string of numbers separated by commas to separate ints or how to save the state of checkboxes?
I am creating a news page. It has two buttons and 4 checkboxes. The first button selects all the checkboxes and the second activates the form. News is displayed page by page (3 per page). When moving to other pages, the selected checkboxes disappear and to remember their state, I decided to use _SESSION . The value of the selected checkboxes is placed in the variable ( _SESSION = 1,2,3,4 ), but now I need to assign these numbers to an array. When outputting, only the number of the first checkbox saves its state. If you assign a variable "hands" numbers separated by commas, then everything works. How to convert terms in int format or tell me another way.
Here is a code example: the session is connected in another file and js too (displayed above).
<script language="JavaScript">
function CheckAll(source)
{
checkboxes = document.getElementsByClassName('checkbox');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = true;
}
}
</script>
<?php
$aDoor = $_POST['checkbox__1']; // Присваиваю данные в переменную
$N = count($aDoor);
for($i=0; $i < $N; $i++)
{
if ($i > 0) {echo(",".$aDoor[$i]); $array13 = (",".$aDoor[$i]);} else {$array13 = ($aDoor[$i]."");
echo $aDoor[$i]."";}
$array15 = $array15 . $array13;
}
echo "</br>";
echo $array15; // Номера выбранных категорий через запятую
$Number_per_page = 1;// Новостей на странице
if ($array15 == NULL) {$array15 = array($_SESSION['array15']); echo "sdas".$_SESSION['array15'];} // Проверяем на выбор элементов
if ($_POST['checkbox__1'] == NULL) {
$_POST['checkbox__1'] = $array15;}
if ($array15 == NULL)
{
if (isset($_GET['page'])) // Если в адрессной строке есть переменная page
$page=($_GET['page']-1); // то уменьшаем на 1
else $page=0;
$start_per_page=abs($page*$Number_per_page); // получаем начало LIMIT (первое число)
$SQL_news_count="SELECT count(*) FROM `news`"; // считаем сколько строк в таблице `news`
$res=mysql_query($SQL_news_count);
$row1=mysql_fetch_row($res); // Получаем строку с числовым индексом
$total_rows=$row1[0]; // Получаем число
echo $total_rows;
$number_pages=ceil($total_rows/$Number_per_page); // Округляем дробь в большую сторону
$query = mysql_query(
"SELECT
`news`.`id` AS `news_id`,
`news`.`name` AS `news_name`,
`news`.`description` AS `news_description`,
`news`.`date` AS `news_date`,
`news`.`type_id` AS `news_type_id`,
`news`.`file_url` AS `news_file`,
`news_type`.`name` AS `news_type_name`,
`news_type`.`color` AS `type_color`
FROM `news`
JOIN `news_type` ON `news_type`.`id` = `news`.`type_id`
LIMIT $start_per_page,$Number_per_page
");
$row = mysql_fetch_array($query); // Получаем записи из таблицы в заданном промежутке
}
else {
if (isset($_GET['page'])) // Если в адрессной строке есть переменная page
$page=($_GET['page']-1); // то уменьшаем на 1
else $page=0;
$start_per_page=abs($page*$Number_per_page); // получаем начало LIMIT (первое число)
$q="SELECT count(*) FROM `news` WHERE
`news`.`type_id` IN (".$array15.")"; // считаем сколько строк в таблице `news` при заданном условии
$res=mysql_query($q);
$row1=mysql_fetch_row($res);// Получаем строку с числовым индексом
$total_rows=$row1[0]; // Получаем число
echo $total_rows;
$number_pages=ceil($total_rows/$Number_per_page); // Округляем дробь в большую сторону
$query = mysql_query(
"SELECT
`news`.`id` AS `news_id`,
`news`.`name` AS `news_name`,
`news`.`description` AS `news_description`,
`news`.`date` AS `news_date`,
`news`.`type_id` AS `news_type_id`,
`news`.`file_url` AS `news_file`,
`news_type`.`name` AS `news_type_name`,
`news_type`.`color` AS `type_color`
FROM `news`
JOIN `news_type` ON `news_type`.`id` = `news`.`type_id`
WHERE
`news`.`type_id` IN (".$array15.")
LIMIT $start_per_page,$Number_per_page
");
$row = mysql_fetch_array($query); // Получаем записи из таблицы в заданном промежутке с условием
}
function checked( $value ) // функция установки флажков
{
$checkboxes = $_POST['checkbox__1']; // Массив выделенных чекбоксов
if ($value > 1 and $value < 6) // Если выбранно флажков больше 1 и менше 6 , то
{
if( in_array( $value, (array)$checkboxes ) ) // Проверяем массив, если он имеется, то
{
return "checked"; // выставляем checked
}
else
{
return false;
}
}
}
$_SESSION['array15'] = $array15;
?>
<div class="news_table">
<div class="news_categories_block">
<form action="news.php" method="POST" name="search">
<div class="news_categories_block_table_left"><input class="news_categories_block_table_button_style" type="button" name="Check_All" value="Check All" onClick="CheckAll(this)"></div>
<div class="news_categories_block_table_button"><button class="news_categories_block_table_button_style" type="submit" >Обновить список</button></div>
<div class="news_categories_block_1">
<div class="news_categories_block_table_left"><div class="news_categories_block_table_center_color" style="background:#2196F3;"></div><div class="checkbox_pos"><input <?=checked(2)?> type="checkbox" class="checkbox" name="checkbox__1[]" value="2" id="check"></div>Реализованные проекты</div>
<div class="news_categories_block_table_center"><div class="news_categories_block_table_center_color" style="background:#009688;"></div><div class="checkbox_pos"><input <?=checked(3)?> type="checkbox" class="checkbox" name="checkbox__1[]" value="3" id="check"></div>Публикации</div>
<div class="news_categories_block_table_center"><div class="news_categories_block_table_center_color" style="background:#CDDC39;"></div><div class="checkbox_pos"><input <?=checked(4)?> type="checkbox" class="checkbox" name="checkbox__1[]" value="4" id="check"></div>Конференции</div>
<div class="news_categories_block_table_right"><div class="news_categories_block_table_center_color" style="background:#9C27B0;"></div><div class="checkbox_pos"><input <?=checked(5)?> type="checkbox" class="checkbox" name="checkbox__1[]" value="5" id="check"></div>Выставки</div>
</div>
</form>
</div>
<div class="news_text_block">
<?php
do
{
$news_id = $row['news_id'];
$news_name = $row['news_name'];
$news_description = $row['news_description'];
$news_date = $row['news_date'];
$news_type_id = $row['news_typ e_id'];
$news_file = $row['news_file'];
$news_type_name = $row['news_type_name'];
$type_color = $row['type_color'];
echo "
<a href='?id=".$news_id."'>
<div class='news_text_block_table'>
<table width='700px' cols='3' border='0' cellspacing='0'>
<tr>
<td width='100px'><div class='news_text_block_table_top_date'>".$news_date."</div></td>
<td><div class='news_text_block_table_top_header'>".$news_name."</div></td>
<td width='7px' rowspan='2' bgcolor='".$type_color."'><div style='min-height:200px;'></div></td>
</tr>
<tr>
<td colspan='2'><div class='news_text_block_table_bottom'>
".$news_description."
</div></td>
</tr>
</table>
</div>
</a>";
}
while ($row = mysql_fetch_array($query));
?>
<div class="news_text_block_table_next"><?php
for($i=1;$i<=$number_pages;$i++) {
if ($i-1 == $page) {
echo $i." ";
} else {
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i."</a> ";
}
}
?></div>
</div>
</div>
Answer the question
In order to leave comments, you need to log in
You have nothing to do, chesslovo. Use the GET method on the form, not POST. And on the page, depending on whether there is a parameter in the attribute line or not, set the checkbox to be checked or not.
For example:
HTML:
<input type="checkbox" name="checkbox1" value="1" <?= !empty($_GET['checkbox1']) ? 'checked="checked"' : null ?> />
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question