I have a form with a drop down. It looks at previous weeks results for an array of teams used. If the team was used it disables the selection on the drop down:
This is the working code:
echo '<select name="survpick" id="survpick">';
$thisweek = (int)getCurrentWeek()-1;
for ($pi=1; $pi<$week; $pi++) {
if ($surv_pick_options_h[$j] == $survpicks[$pi]) {
$noshowhome = true;
}
if ($surv_pick_options_v[$j] == $survpicks[$pi]) {
$noshowvisitor = true;
}
}
if ($noshowhome) {
echo '<option value="" style="background-color:pink" disabled>'.$surv_pick_options_h[$j].'</option>';
} else {
if ($currentID == $surv_pick_options_h[$j]) {
echo '<option value="'.$surv_pick_options_h[$j].'" selected>'.$surv_pick_options_h[$j].'</option>';
} else {
echo '<option value="'.$surv_pick_options_h[$j].'" >'.$surv_pick_options_h[$j].'</option>';
}
}
if ($noshowvisitor) {
echo '<option value="" style="background-color:pink" disabled> '.$surv_pick_options_v[$j].'</option>';
}
else {
if ($currentID == $surv_pick_options_v[$j]) {
echo '<option value="'.$surv_pick_options_v[$j].'" selected>'.$surv_pick_options_v[$j].'</option>';
}
else {
echo '<option value="'.$surv_pick_options_v[$j].'" >'.$surv_pick_options_v[$j].'</option>';
}
}
echo '</select>';
I have 2 more arrays, one for current week home teams that have expired and one for current week visitor teams that expired. Somehow I need to account for those current week expired teams in the calculation. Since the for loop is only looking at previous weeks, i could not find a way to merge. I tried inside the loop and outside the loop. Is this even possible?
Array ( $survpicks)
[1] => DET
[2] => TB
[3] => NE
[5] => JAX
[4] => PHI
[6] => IND
Array ( $surv_pick_expired_h)
[0] => KC
Array ($surv_pick_expired_v)
[0] => OAK
There is also a chance that one of the $survpicks could also be in one of the $surv_pick_expired_h or $surv_pick_expired_v arrays. Right now the drop down shows the values from $survpicks as disabled. I need to be able to also include the values from $surv_pick_expired_h and $surv_pick_expired_v arrays as disabled
I don't fully understand your question, but may the array_merge() and in_array() functions help?
$toDisable = array_merge($survpicks, $surv_pick_expired_h, $surv_pick_expired_v);
foreach($all as $something) {
if(in_array($something, $toDisable)) {
// disable
} else {
// enable
}
}
I hope this helps somewhat:
// this will contain all the survpicks up to last week,
// so now this week is not disabled
$survpicks_to_disable = array_slice($survpicks,0,$week-1);
// all the expired teams are to be disabled
$disabled = array_merge($survpicks_to_disable,$surv_pick_expired_h,$surv_pick_expired_v);
echo '<select name="survpick" id="survpick">
<option default>Make Selection</option>';
foreach($surv_pick_options_h as $home){
echo '<option value="'.$home.'"'.(in_array($home,$disabled)?' style="background-color:pink" disabled':'').($home == $currentID?' selected':'').'>'.$home.'</option>';
}
foreach($surv_pick_options_v as $visitor){
echo '<option value="'.$visitor.'"'.(in_array($visitor,$disabled)?' style="background-color:pink" disabled':'').($visitor == $currentID?' selected':'').'>'.$visitor.'</option>';
}
echo '</select>';
Why not put this into your style, so you don't need the inline styles:
#survpick option[disabled] {
background-color: pink;
}
Related
I have a tables in MySQL one is test table columns like this
id refid name userdefined
1 0 A
2 0 B
3 0 C
4 1 A1 abc
5 1 A2 cde
6 2 B1
7 2 B2
8 3 C3
9 3 c4
10 3 c5
11 2 C7
12 2 C8 lmn
13 11 c9
14 11 c10
Using the above table I am creating the dynamic menu using a PHP function.
I have one more table, it has the login fields and data like this:
id username password field3
1 john john 1,3,4,5,6,7,8,9
What i want is if John is logged in, how do I only show the menu items in field3.
I am new to PHP. I am showing all menu items using a function in
PHP please help me, thanks in advance.
<?php
//this is php function for creating menu
$sql2 = mysqli_query($conn, 'select * from login');
$row = mysqli_fetch_array($sql2);
$menu_items = explode(',', $row['field3']);
function submenu($parentid = 0)
{
global $conn;
$sql = mysqli_query($conn, "SELECT * FROM test WHERE refid=" . $parentid . " AND id IN " . ($menu_items));
{
$rowcount = mysqli_num_rows($sql);
if ($rowcount > 0) {
echo '<ul>';
}
while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
if ($row['refid'] == 0) {
echo '<li class="limain">' . $row['name'];
submenu($row['id']);
echo '</li>';
} else {
if ($row['userdefined']) {
echo '<li class="lichild">' . $row['name'] . '';
} else {
echo '<li class="lichild">' . $row['name'];
}
submenu($row['id']);
echo '</li>';
}
}
if ($rowcount > 0) {
echo '</ul>';
}
}
}
?>
$sql2=mysqli_query($conn,'select * from login');
$row = mysqli_fetch_array($sql2);
//storing the field values in menuitems variables
$menu_items = $row['field3'];
//pass the menuitem variables to query
function submenu($parentid=0){
global $conn;
$sql=mysqli_query($conn,"SELECT * FROM test WHERE refid=".$parentid ." AND
id in ($menu_items)");
{
$rowcount=mysqli_num_rows($sql);
if($rowcount>0){
echo '<ul>';
}
while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC))
{
if($row['refid']==0)
{
echo '<li class="limain">'.$row['name'];
submenu($row['id']);
echo '</li>';
}
else{
if($row['userdefined']){
echo '<li class="lichild">'.$row['name'].'';
}else{
echo '<li class="lichild">'.$row['name'];
}
submenu($row['id']);
echo '</li>';
}
}
if($rowcount>0){
echo '</ul>';
}
}
}
Your function is mostly correct in principle, I had a bit of trouble following your function so I changed a few things to improve readability, but this should work.
The only issue I found that would prevent this from working was your if($refid), which is checking for any data not == to null.
0 == null //true 0 === null //false
If I understood your table correctly, entries with a refid of 0 are parent items. In that case they're the only menu items passing into that part of the function, so I changed it from if ($refid) to if ($refid === 0)
With regards to you wanting to you only wanting to show specific menu items, you'll need to run a separate query outside of the function to get the list you made. And then add a check in you submenu() function.
I didn't have a way to test this, but it should work.
//Query to get list of menu items
$menu_items = explode(',', $row['menus']);
function submenu($parentid=0, $menu_items=null) {
global $conn;
$sql=mysqli_query($conn,"SELECT * FROM test WHERE refid=".$parentid);
$rowcount=mysqli_num_rows($sql);
if($rowcount > 0) {
echo '<ul>';
}
while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC)) {
if($menu_items !== null || !in_array($id, $menu_items)) {
continue;
}
$id = $row['id'];
$refid = $row['refid'];
$userdefined = $row['userdefined'];
$name = $row['name'];
if($refid === 0) {
echo "<li class='limain'>{$name}";
submenu($id, $menu_items);
echo '</li>';
} else if ($refid > 0) {
if($userdefined) {
echo "<li class='lichild'><a href='{$userdefined}'>{$name}</a>";
} else {
echo "<li class='lichild'>{$name}";
}
submenu($id, $menu_items);
echo '</li>';
}
}
if($rowcount > 0) {
echo '</ul>';
}
}
You must protect the php files too, not only the menu items. If the user know the php filename, can access with a direct link to protected area.
I am in the process of building a software download site for my company.
However, I have come across a problem that I am unsure how to get past.
I am retrieving all the information from a table about a particular software release and placing them in a multidimensional array. I am then trying to run a foreach (I have even tried a for loop) against that array and I get an error shown below:
When I run var dump against the original array, I get this:
So I am really confused as I don't know what I'm missing or where I am going wrong. The reason why I want to run this is so that I can filter the array into a one dimensional array.
Below is the code for the main web page
<?php
//Displays list of companies in 2 columns
$versionAccess = VersionAccess::findAccess($relId);//This gets set earlier in the webpage
$count = count($versionAccess);
var_dump($versionAccess);
foreach ($versionAccess as $va)
{
if ($va->company_access != '0')
{
$versionAcc[] = $va->comapny_id;
}
}
foreach ($company as $compAccess)
{
$compAccessId = $compAccess->company_id;
if (in_array($compAccessId, $versionAcc))
{
$access = 'checked disabled';
}
else { $access = 'disabled'; }
$accessName = 'access'.$compAccessId;
if ($ctr % 2 == 0)
{
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
else
{
if ($ctr < $compCount)
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
}
else
{
echo '<tr>';
echo '<td>'.$compAccess->company_name.':</td>';
echo '<td><label class="switch"><input type="checkbox" name="'.$accessName.'" value="1" '.$access.'><span class="slider round"></span></label></td>';
echo '</tr>';
}
}
$ctr++;
}
?>
The function that brings the data from the database is:
public static function findAccess($accessId)
//find the version access in database
{
return self::findQuery("SELECT * FROM version_access WHERE version_id = '$accessId'");
}
The findQuery method:
public static function findQuery($sql)
{
global $database;
$resultSet = $database->query($sql);
$objectArray = array();
while ($row = mysqli_fetch_array($resultSet))
{
$objectArray[] = self::instant($row);
}
return $objectArray;
}
I am still relatively new and any help is greatly appreciated.
Im trying few hours repair my code but its always fail.
I have array in file:
$liquidyLista7 = array(
'Arbuz' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
),
// and much more...
... i want to echo all values from each arrays but my "bad epic" code not work :/
function pokazOpisyVolish($liquidyLista7)
{
$i = 0;
$select = '<div id="volishPage'.$i.'" class="tab-pane fade"><ul class="comment-list">';
foreach($liquidyLista7 as $key => $record) {
$i++;
if (is_array($record)) {
$select .= '<li><p class="desc">'.$key.'</p>';
$select .= pokazOpisyVolish($record);
$select .= '</li>';
} else {
$select .= '<li><p class="desc">'.$record.'</p></li>';
}
}
$select .= '</ul></div>';
return $select;
echo pokazOpisyVolish($liquidyLista7);
Pls visit my test website and check how it works in "Mr Jack" (simple HTML - not PHP) but in "Volish" is my PHP code... :(
may be you are looking for some thing like this. the link you gave
uses plugin and css to display the result onto the webpage,
i can show you how to do it, but after that you have use your own css to display the result, i am thinking you might have add some div
name
just run this code in separate file, and you are good, understand what's happening and than add the div
<?php
$liquidyLista7 = array(
'Arbuz' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
),
'newArtist' => array(
'Wyraźny arbuz, soczysty arbuz, naprawde chyba najlepszy z oferty',
'Delikatny arbuz.',
'odświeżajacy arbuz',
'Bardzo delikatny, mniej wyrażny arbuz'
)
);
//get the link from url
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
foreach ($liquidyLista7 as $key => $record) {
//ad artist name to the current url
echo "<a href=$actual_link?artistName=$key>$key</a><br>";
if(isset($_GET['artistName'])){
if(is_array($record)){
echo "<ul>";
foreach($liquidyLista7[$key] as $value)
if($_GET['artistName']==$key)
echo "<li>$value</li>";
}
echo "</ul>";
}
}
?>
I have an excel sheet which link is given below.
In this sheet I gave 3 sections of cse.
I want to display them in php and when i select the date from date picker and section from drop down that section presented and absentees should be display. I don't know how to do this.
$students = [
['CSE-A' => 'PRESENT'],
['CSE-A' => 'PRESENT'],
['CSE-B' => 'ABSENT'],
['CSE-B' => 'ABSENT'],
['CSE-A' => 'ABSENT'],
['CSE-B' => 'PRESENT'],
['CSE-B' => 'ABSENT'],
['CSE-A' => 'ABSENT'],
['CSE-C' => 'ABSENT'],
['CSE-C' => 'PRESENT']
];
$outputs = [];
foreach($students as $array) {
foreach($array as $key => $value) {
if (!isset($outputs[$key])) {
$outputs[$key] = [
'present' => 0,
'absent' => 0,
];
}
//check value
if ($array[$key] === 'PRESENT') {
$outputs[$key]['present']++;
} else if ($array[$key] === 'ABSENT') {
$outputs[$key]['absent']++;
}
}
}
foreach($outputs as $key => $value) {
echo $key.
"\n";
echo "Present Totals: ".$value['present'].
"\n";
echo "absent Totals: ".$value['absent'].
"\n";
echo "--------------\n";
}
I tried up to this. But I am confusing when date from datepicker and section from dropdown selected how to display that date students presences and absences on selected option.
Ok. The code you need is below. I tested it and it works.
Explanation: It crates a div for every match of dates and cse that exists in the table, which includes the nuber of presentiew and the number of absenties. Then, using javascript, when a selection is choosed, it checks if both the date and the cse selection is chosen. If yes, it makes the div with the selected date and cse visible. If there isn't the corresponding div (because the array doesn't have a student for that cse in that day) an appropriate message is show. Otherwise the data for the selected date and cse are shown.
What it does is also included in the comments. If you have any question or you wanted something different please let me know.
<html>
<?php
$students = array
(
array('CSE-A','PRESENT',"2-10-2017"),
array('CSE-A','PRESENT',"2-10-2017"),
array('CSE-B','ABSENT',"3-10-2017"),
array('CSE-B','ABSENT',"3-10-2017"),
array('CSE-A','ABSENT',"3-10-2017"),
array('CSE-B','PRESENT',"4-10-2017"),
array('CSE-B','ABSENT',"4-10-2017"),
array('CSE-A','ABSENT',"5-10-2017"),
array('CSE-C','ABSENT',"5-10-2017"),
array('CSE-C','PRESENT',"5-10-2017")
);
$outputs = [];
foreach($students as $array) {
$cse = $array[0];
$present = $array[1];
$date = $array[2];
$entry = $cse.", ".$date;
if (!isset($outputs[$entry])) {
$outputs[$entry] = [
'present' => 0,
'absent' => 0,
];
}
//check value
if ($present === 'PRESENT') {
$outputs[$entry]['present']++;
} else if ($present === 'ABSENT') {
$outputs[$entry]['absent']++;
}
}
// Save all availbale cse, for the dropdown choices
$all_cse = array();
foreach($students as $array) {
$cse = $array[0];
array_push($all_cse,$cse);
}
$all_cse = array_combine($all_cse, $all_cse);
// Save all availbale dates, for the dropdown choices
$all_dates = array();
foreach($students as $array) {
$date = $array[2];
array_push($all_dates,$date);
}
$all_dates = array_combine($all_dates, $all_dates);
?>
<body>
<select id="cse" onchange="myFunction()">
<option disabled selected value> -- select an option -- </option>
<?php
foreach($all_cse as $cse) {
echo "<option value='".$cse."'>".$cse."</option>";
}
?>
</select>
<select id="dates" onchange="myFunction()">
<option disabled selected value> -- select an option -- </option>
<?php
foreach($all_dates as $date) {
echo "<option value='".$date."'>".$date."</option>";
}
?>
</select>
<br><br>
<div id="data">
</div>
<?php
foreach($outputs as $key => $value) {
// Create a div for every match of cse and date that exists.
echo "<div id='".$key."' style='display:none;'>";
"<br>";
echo "Present Totals: ".$value['present'].
"<br>";
echo "absent Totals: ".$value['absent'].
"<br>";
echo "</div>";
}
?>
</body>
</html>
<script type="text/javascript">
var lastDiv;
function myFunction() {
var date = document.getElementById("dates").value;
var cse = document.getElementById("cse").value;
if (cse!="" && date!=""){
// Make invisible the div for the previous choice
if (lastDiv!=null){
lastDiv.style.display = "none";
}
lastDiv = document.getElementById(cse + ", " + date);
// Make visible the div for the current choice
if (lastDiv!=null){
document.getElementById("data").innerHTML = cse +"<br>Date: " + date;
lastDiv.style.display = "block";
} else {
document.getElementById("data").innerHTML = "No matches found for this cse in this date.";
}
}
}
</script>
I have a full list of timezones in a select menu like so:
<option value="Pacific/Kosrae"> Pacific/Kosrae( +11:00 GMT ) </option>
<option value="Pacific/Kwajalein"> Pacific/Kwajalein( +12:00 GMT ) </option>
<option value="Pacific/Majuro"> Pacific/Majuro( +12:00 GMT ) </option>
<option value="Pacific/Marquesas"> Pacific/Marquesas( -09:30 GMT ) </option>
<option value="Pacific/Midway"> Pacific/Midway( -11:00 GMT ) </option>
the list goes on forever.
I want to change each of the options into this format:
if($_SESSION['timezone'] == 'Africa/Abidjan') {
echo '<option selected="selected" value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>';
} else {
echo '<option value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>';
}
How can I use php to avoid having to copy paste and edit each of the options manually??
Store the data in some data structure, and use a loop. For example, using a map from timezone name to offset:
$timezones = array(
'Pacific/Kosrae' => '+11:00',
'Pacific/Kwajalein' => '+12:00',
...
);
foreach($timezones as $name => $offset) {
echo "<option value=\"$name\"" . ($name == $_SESSION['timezone'] ? " selected" : "") . ">$name( $offset GMT ) </option>\n";
}
Ok, imagine you have a variable containing the form above, let's call it $form and another variable containing i.e. 'Africa/Abidjan' - $timezone.
$pattern = '/="'.str_replace('/', '\/', $timezone).'"/'; # /="Africa\/Abidjan"/
$replacement = '="'.$timezone.'" selected="selected"'; # ="Africa/Abidjan" selected="selected"
$output_form = preg_replace($pattern, $replacement, $form);
Haven't actually tested it, but it should work.
$cur_timezone = 'Africa/Abidjan';
$timezones_arr = array ('Pacific/Kosrae','Pacific/Kwajalein',...);
$times_arr = array ('+11:00 GMT', '+12:00 GMT',...);
for ($i = 0; $i < count ($timezones_arr); $i ++) {
if ($timezones_arr[$i] == $cur_timezone) {
echo '<option selected="selected" value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
}
else {
echo '<option value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
}
}
You must change only variable $cur_timezone. For every element of $timezones_arr must exist elemnt of $times_arr.
Ok, you don't have to it with DOM, but since there is already plenty of other answers showing different approaches, here is how to do it with DOM:
function timezoneHelper($selected = NULL)
{
$dom = new DOMDocument;
$dom->formatOutput = TRUE;
$dom->loadXML('<select/>');
$dom->documentElement->setAttribute('name', 'timezone-selector');
$timezones = DateTimeZone::listIdentifiers();
if(!is_numeric($selected)) {
$selected = array_search($selected, $timezones);
}
foreach($timezones as $id => $timezone) {
$option = $dom->createElement('option', $timezone);
$option->setAttribute('value', $id);
if($id == $selected) {
$option->setAttribute('selected', 'selected');
}
$dom->documentElement->appendChild($option);
unset($option);
}
return $dom->saveXML($dom->documentElement);
}
The above will create a list of timezone identifiers (w\out the GMT diff though. See DateTimeZone::listAbbreviations if you need them) as <option> elements in a <select> element. Unlike your code in the question, the value used for the value attribute is the numeric offset in the array returned by DateTimeZone::listIdentifiers(); instead of the timezone identifier itself. You can invoke the helper with the timezone identifier or the numeric ID though, e.g.
echo timezoneHelper('551');
// or
echo timezoneHelper('Zulu');
would both markup
<option value="551" selected="selected">Zulu</option>
with the selected attribute in the list returned.