Jump to content

Search the Community

Showing results for tags 'array'.

More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


  • W3Schools
    • General
    • Suggestions
    • Critiques
  • HTML Forums
    • CSS
  • Browser Scripting
    • JavaScript
    • VBScript
  • Server Scripting
    • Web Servers
    • Version Control
    • SQL
    • ASP
    • PHP
    • .NET
    • ColdFusion
    • Java/JSP/J2EE
    • CGI
  • XML Forums
    • XML
    • Schema
    • Web Services
  • Multimedia
    • Multimedia
    • FLASH


  • Community Calendar




Website URL








Found 51 results

  1. Loop in Loop with Array

    I was try make loop in loop with array. it should be like: L1: T1&C1---L1: T2&C2---L1: T3&C3--- L2: T1&C1---L2: T2&C2---L2: T3&C3--- L3: T1&C1---L3: T2&C2---L3: T3&C3--- But I got other than that i thought, so what did i wrong?
  2. Hello, I've just read the tutorial on how to process a form in PHP, available on this site, and I was wondering if there's a specific reason to declare empty variables (or arrays) at the beginning? I don't understand what is the point of doing so. For example, in the following code, why would the 2sd and 3rd lines be necessary? <?php $errors = []; $name = $email = $message = ''; if (condition) { $errors['name'] = 'alert'; } else { $name = sanitize_input($_POST['name']); if (new_condition, $name) { $errors['name'] = 'new alert'; } }
  3. Associative Array

    Hi there, Been trying to learn PHP in my spare time the last few weeks. Setting myself little tasks, challenges, mini projects etc. but I'm quite stuck on this one. I'm trying to create an associative array to hold the following information on monthly rainfall in millimetres (jpeg of table attached below). I then want to find the minimum, maximum and average rainfall per month and per year. I've created two arrays experimenting but only the second one is associative (I've attached the php file below as well), but here is the code anyways... <?php $multidimensional_array = array(array("Jan", 292.9, 147.1, 175.9, 130.6, 107.7), array("Feb", 53.7, 154.9, 195.1, 91.1, 144.6), array("Mar", 92.8, 72.3, 87.2, 106.8, 64.4), array("Apr", 64.2, 55.4, 38.3, 63.6, 42.0), array("May", 135.3, 128.6, 90.4, 111.2, 45.4), array("June", 37.1, 63.4, 40.5, 27.2, 68.0), array("July", 76.5, 31.6, 35.8, 82.7, 66.9), array("Aug", 97.1, 153.8, 90.0, 93.4, 107.4), array("Sep", 39.2, 16.1, 72.4, 87.9, 88.5), array("Oct", 57.6, 125.2, 105.2, 49.7, 35.3), array("Nov", 184.4, 154.4, 129.3, 190.5, 69.2), array("Dec", 274.3, 100.7, 110.2, 233.2, 74.0), ); $multidimensionalAss_array = array( array("month"=>"Jan", "2012"=>92.9, "2013"=>147.1, "2014"=>175.9, "2015"=>130.6, "2016"=>107.7), array("month"=>"Feb", "2012"=>53.7, "2013"=>154.9, "2014"=>195.1, "2015"=>91.1, "2016"=>144.6), array("month"=>"Mar", "2012"=>92.8, "2013"=>72.3, "2014"=>87.2, "2015"=>106.8, "2016"=>64.4), array("month"=>"Apr", "2012"=>64.2, "2013"=>55.4, "2014"=>38.3, "2015"=>63.6, "2016"=>42.0), array("month"=>"May", "2012"=>135.3, "2013"=>128.6, "2014"=>90.4, "2015"=>111.2, "2016"=>45.4), array("month"=>"June", "2012"=>37.1, "2013"=>63.4, "2014"=>40.5, "2015"=>27.2, "2016"=>68.0), array("month"=>"July", "2012"=>76.5, "2013"=>31.6, "2014"=>35.8, "2015"=>82.7, "2016"=>66.9), array("month"=>"Aug", "2012"=>97.1, "2013"=>153.8, "2014"=>90.0, "2015"=>93.4, "2016"=>107.4), array("month"=>"Sep", "2012"=>39.2, "2013"=>16.1, "2014"=>72.4, "2015"=>87.9, "2016"=>88.5), array("month"=>"Oct", "2012"=>57.6, "2013"=>125.2, "2014"=>105.2, "2015"=>49.7, "2016"=>35.3), array("month"=>"Nov", "2012"=>184.4, "2013"=>154.4, "2014"=>129.3, "2015"=>190.5, "2016"=>69.2), array("month"=>"Dec", "2012"=>274.3, "2013"=>100.7, "2014"=>110.2, "2015"=>233.2, "2016"=>74.0), ); //print_r($multidimensional_array); print_r($multidimensionalAss_array); ?> Could anyone give me some advice on where I'd go next with the associative array? I'm fairly sure I can't loop through it no? I'm fairly stuck on this and it's annoying me quiet a bit! Finding JavaScript easier to learn for sure... haha Thanks, Fintan table challenge.php
  4. Array index by its value (key)

    How to get an array index by its value (key)? Is there a special function for that in PHP? For example, I have an array $a ["zero","one","two"] and a variable $b = "two", I know the value is in the array, but I don't know its index.
  5. So this generates 3 arrays with 5 numbers in each one. How would I go about checking each array to see if there are any duplicate numbers in an array?
  6. I found an online function called GetDays and I slightly modified it to calculate the number of days between a past date and the current time. The function works fine for 1 input, but I have a table full of records and want to use the function to get the total days for each record separatedly. <?php // function GetDays($sStartDate, $sEndDate){ // Firstly, format the provided dates. // This function works best with YYYY-MM-DD // but other date formats will work thanks // to strtotime(). $sStartDate = gmdate("Y-m-d", strtotime($sStartDate)); $sEndDate = gmdate("Y-m-d", strtotime($sEndDate)); // Start the variable off with the start date $aDays[] = $sStartDate; // Set a 'temp' variable, sCurrentDate, with // the start date - before beginning the loop $sCurrentDate = $sStartDate; // While the current date is less than the end date while($sCurrentDate < $sEndDate){ // Add a day to the current date $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate))); // Add this new day to the aDays array global $aDays; // deze global heb ik zelf toegevoegd en MOET hier staan, iig niet buiten deze conditie global $tot_days; $aDays[] = $sCurrentDate; } $tot_days = count($aDays); $tot_days = $tot_days +1; return $tot_days; } // function ends // // here the records are fetched from the database table while($row = $result->fetch_assoc()){ echo "<center>"; $array = implode(" / " , $row); $sStartDate = $row['created_on']; $sEndDate = date("Y-m-d"); GetDays ($sStartDate, $sEndDate); echo "</center>"; ?> <center> <form action="#" method="post" > <input type="text" name="test" value="<?php echo $array . ' / ' . $sEndDate . ' / ' . $tot_days ; ?>" size="60"/> </center> <?php } // rest of the code to complete the form --> ?>
  7. Hi everybody http://www.w3schools.com/js/js_array_sort.asp If I'm right, the JS-Tutorial about sorting arrays randomly has a lack. The order of the array is kind of in a random order at the first glance, but the elements often stay at the same place as they were. This is true in the most for the element at the end of the array and the effect happens in most browsers, but not in all of them. I've written a script to test this. In these browsers the elements are not evenly distributed as they should: Chrome, IE, Edge, Opera. In Safari they were evenly distributed when I tried it. Try it yourself. <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var sum; var points; var i, j; sum = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; for (i=0;i<1000;i++){ points = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]; points.sort(function(a, {return 0.5 - Math.random()}); for (j=0;j<points.length;j++){ sum[j]+=points[j]; } } document.getElementById("demo").innerHTML = sum; </script> </body> </html> What the code does: It creates an array with a leading 1 and a 1 at the last place and zeroes between (points). Then it sorts this array randomly and adds the resulting array to a sum array. Then it starts again with the specific array. The generating, sorting and adding is done a 1000 times. When you reload the page the summed up numbers are displayed. Result: In many browsers the last number is higher than the others, which shouldn't happen with correct random sorting. Cheers, celus
  8. I am working on a project for my job. I have php that creates multiple radio button lists through an array. There are 6 categories that that a list of radio buttons for each of the categories. I am trying to add an onclick javascript function to just one of the radio buttons. There is a text box on the very bottom, I want that to only show up if a certain radio button is clicked. I have the code attached below <?php $form_fields = array( "id" => array( "bsc_id", "fname", "mname", "lname", "email", "grad_id", "major_id", "grad_date", "deg_id", "age_id", "ethn_id", "res_id", "assist_id", "assist_other" ) , "name" => array( "Banner ID", "First Name", "Middle Name", "Last Name", "E-mail Address", "Status", "Major", "Graduation Date<br/>(mm-yyyy)", "Degree", "Age Group", "Ethnicity", "Residency", "Greatest Assistance", "If Assistance Other" ) , "type" => array( "age_id" => "radio", "grad_id" => "radio", "major_id" => "select", "deg_id" => "radio", "ethn_id" => "radio", "grad_date" => "date", "res_id" => "checkbox", "assist_id" => "radio" ) , "option" => array( "age_id" => "sql:select * from age_code", "grad_id" => "sql:select * from grad_code", "major_id" => "sql:select * from major_code ORDER BY major_text", "deg_id" => "sql:select * from deg_code", "ethn_id" => "sql:select * from ethn_code", "grad_date" => "fields:m-Y", "res_id" => "sql:select * from res_code", "assist_id" => "sql:select * from assist_code" ) );
  9. Down here the code to call a function it works with list. I dont understand how list works, In my example list gives a survey of 2 arrays that are executed inside the function here is how I call the function: $row = list ($variable, $number) = my_function(); var_dump($row); the output is like this: I need the values separated but how can I list the second array (the one with the numbers) as single numbers outside an own function?
  10. foreach loop with list / array

    My goal is to loop with foreach through two variables. So I found this script; <?php $abc = array('Mark','Laura'); $add = array('cocktails','champagne'); $array = array($abc,$add); foreach ($array as list($arr1, $arr2)) { // echo '<br>'; echo '<br>'; echo $arr1; echo '<br>'; echo $arr2; } But the output is not wat is desired. It says: And I would rather have: Is there any function / command in the php array reference to do this or should other code be used?
  11. Hello, I'm currently learning JavaScript and I plan to work a lot with arrays. I store all arrays in external files and use JSON to access them. Now I wanted to read the names of all arrays that are nested in one main array to create a navigation list but I don't know how to do that. This is an example of a JSON file I use: {"MainArray":[ {"SubArray1":[{ ... }] }, {"SubArray2":[{ ... }] }, {"SubArray3":[{ ... }] } ]} How do I get the names of all SubArrays? I tried this // arr is a JSON-parsed string function myFunction(arr) { var out = ""; var i; for(i = 0; i < arr.MainArray.length; i++) { out += arr.MainArray[i].toString() + '<br>'; } document.getElementById("aso").innerHTML = out; } but it only creates a list of "[object Object]" items. Should I create another JSON file where all array names are listed instead?
  12. I used AJAX to retrieve concatenated data from a XML document. Now I need to store that data into a Javascript array. How do I do this? My Javascript Code: var SVG_Data var Retrieved_Data var Coordinate_Pair var Element_List var Counter function Setup() { SVG_Data = new XMLHttpRequest(); SVG_Data.open("GET","http://localhost:8080/exist/rest/db/apps/HTML_Student/Database_Retrieval.xq", true); SVG_Data.onreadystatechange = function () { if (SVG_Data.readyState = 4) { Retrieved_Data = SVG_Data.responseText; document.getElementById("Information").value = Retrieved_Data; Coordinate_Pair = new Array(); Coordinate_Pair.push.apply(Retrieved_Data); Element_List = new Array("Top_Ladder_Line","Middle_Ladder_Line", "Bottom_Ladder_Line"); for (Counter = 0; Counter < 3; Counter++) { document.getElementById("Information").value = Coordinate_Pair[Counter]; document.getElementById(Element_List[Counter]).setAttribute("points", Coordinate_Pair[Counter]);} } } SVG_Data.send(); } The concatenated data I received from the eXist database: "60,30 60,80 150,80 150,30", "60,130 60,80 150,80 150,130", "60,180 60,130 150,130 150,180"
  13. I have: function val(a,z){ return a/2, z+8; } val(3,4); Put this in a Array; x = 1,5 y = 12 [x,y]; Thanks.
  14. The idea is to insert with the browser a multiple input field, a loop, into mysql database. Is the solution to first put this loop input variable in an array and after that create in this case multible tables? Or is an array not necessary?
  15. I want to add elements by the browser into an array with the name $map I wrote the next piece of code for this: <?php $var = $_POST["var"]; function addIntoArray($var) { global $map; if (($var != ".") || ($var != "..")) { array_push($map, $var); echo $var . " succesfully added to the array!"; } return; } ?> The problem though, is not having an error code, but the screen just does not give an output at all while I did make an echo line. Whats going wrong here?
  16. Array problem

    Hello i try make it like: <html><head> <script type="text/javascript"> function Code() { A=["T1","T2","T3",4,2,1]; var B1=A;var B2=A;var B3=A; if (A[0]=="T2") {B1[3]=5;} if (A[1]=="T2") {B2[3]=5;} if (A[2]=="T2") {B3[3]=5;} document.getElementById('W1').innerHTML="B1:"+B1[3] document.getElementById('W2').innerHTML="B2:"+B2[3]; document.getElementById('W3').innerHTML="B3:"+B3[3]; } </script></head><body> <input type="button" value="Code it!" onClick="Code()"><br><br><br> <div id="W1"></div> <div id="W2"></div> <div id="W3"></div> </body> when i press button then i got 5 on all B1, B2 and B3. but it should be only B2 have 5 while other got 4. Why it going wrong?
  17. Alright, so I am making a program that when a button that says "yo" for example, will display a prompt when click. If the answer to the prompt is a verb, then the "yo" form of that verb is displayed. So far I have a button, that when clicked has a prompt. When I enter a word in the button I can't make it display the "yo" form of the verb. Please help: Here is my javascript: function verb(verb, yo) { this.verb = verb; this.yo = yo;}//*All verbsvar estar = new verb("estar", "estoy");//*All arraysvar verbs = [estar];//*Verb translation present_yofunction presentYo(){ var verb = verbs; var answer = prompt("Enter a verb: ").toLowerCase(); if (answer == verb){ document.getElementById("translation").innerHTML = "verbs.yo"; } else{ document.getElementById("translation").innerHTML = "I do not know the translation."; } } When I enter a word, all that shows up is "I do not know the translation." Please help. Thanks.
  18. Hello so I made an array and it works fine when you refresh the page the image changes but at lower screen resolutions the image cuts of the edge. With my basic code it worked fine I was wondering what I was doing wrong. This was my old code which was just one image and at lower resolutions it would still fit on the screen <center><img src="my image url" width=47%> </center> But when I use my array it cuts of I want to make the image center and just put width=47% like the top code but not sure where to put it <script language="JavaScript"><!--function random_imglink(){var myimages=new Array()myimages[1]="my image url"myimages[2]="my image url"myimages[3]="my image url"myimages[4]="my image url"myimages[5]="my image url"myimages[6]="my image url"var ry=Math.floor(Math.random()*myimages.length)if (ry==0)ry=1document.write('<img src="'+myimages[ry]+'" width=47%')}random_imglink()//--></script> If you could help me it would be much appreciated
  19. array problems

    Newbie, first time ever posting. I found this script online and it works exactly as I want with the exception of sorting. On my local host it presents output in alphabetical order, but online it will not. I've tried for days to work it into an array so I could sort it, but have hit my patience wall. Can someone please show me where to insert the array and then read it back? Thanks in advance for any effort put into this topic. George dir-read-script.php
  20. innerHTML Expression Help

    Hi W3, this array-within-array should print out Students 1, 2, 3 and their averaged grades, but it's only printing out Student 3 & average, and no console errors. It works fine substituting console.log, so the problem is my innerHTML part. Thanks in advance for direction. document.addEventListener("DOMContentLoaded", function(){var grades = [[89, 77, 78], [76, 82, 81], [91, 94, 89]];var total = 0;var average = 0.0;for (var row = 0; row < grades.length; row++){ for (var col = 0; col < grades[row].length; col++){ total += grades[row][col];} average = total / grades[row].length; divid = document.getElementById("nice"); divid.innerHTML = 'Student ' + parseInt(row+1) + 'average: ' + average.toFixed(2); total = 0; average = 0.0;}});
  21. Echoing Sorted Array With <Option> Tag

    Hi everyone. I'm trying to echo the values of an array inside HTML's <option> tag. The following should be echod: <option value="Moose Jaw,SK,Canada">Moose Jaw,SK,Canada</option> <option value="Prince Albert,SK,Canada">Prince Albert,SK,Canada</option> <option value="Regina,SK,Canada">Regina,SK,Canada</option> <option value="Saskatoon,SK,Canada">Saskatoon,SK,Canada</option> The code I attempted was: <!DOCTYPE html> <html> <body> <?php $sk=array("Saskatoon","Regina","Moose Jaw", "Prince Albert"); sort($sk); foreach ($sk as $value) { echo '<option value="'.$value.',SK,Canada">'.$value.',SK,Canada</option>'; echo "<br>"; } ?> </body> </html> Pls. note that the values of the array are not sorted alphabetically. The output, however, should be sorted in ascending format and be within the <option> tag after adding the province and country. Though the select element will be there in the final code, I ignored it here because I am able to manage it separately. I now want to do what I explained only, which I am unable to yet. Any help will be highly appreciated.
  22. i am trying to create new element that contains a php returned array in it. $( "#invoice" ).append( '<td id="item"><script>getData("items.php", "item")</script></td>'); the array is returned from 'items.php' and is called with simple ajax technique. its not working, or simply i don't know how can i do that. can anyone please guide me or suggest me some better technique...?
  23. I have "Checkbox" input with multiple values. <input type="checkbox" name="drug" value="1" /><input type="checkbox" name="drug" value="2" /><input type="checkbox" name="drug" value="3" /> I want to run through this array and alert() all the values. I'm guessing I should use Jquery's .each() command but I'm not sure how to use it. the problem is, I don't understand how to use it so I can get the key and it's value. $.each($("input[name='drug']"),function(i,v){ alert(i+": "+v);}); What am I missing in the main idea?
  24. So I have a calendar that I created using JavaScript on an HTML page but now I need to create a variable that contains an array. The array needs to be a week long to-do list. Problem 1, I try to create the variable to produce the 'to-do' list but I'm not sure how to associate the array with a week out of the month. Second, I'm not sure where to plug in the variable once I have created it in order to make it show up in the calendar. Below is the script I wrote for the calendar. And help would be appreciated. Thanks! <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Month Calendar</title> </head> <body> <script> monthnames = new Array( "January", "Februrary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decemeber"); var linkcount=0; function addlink(month, day, href) { var entry = new Array(3); entry[0] = month; entry[1] = day; entry[2] = href; this[linkcount++] = entry; } Array.prototype.addlink = addlink; linkdays = new Array(); monthdays = new Array(12); monthdays[0]=31; monthdays[1]=28; monthdays[2]=31; monthdays[3]=30; monthdays[4]=31; monthdays[5]=30; monthdays[6]=31; monthdays[7]=31; monthdays[8]=30; monthdays[9]=31; monthdays[10]=30; monthdays[11]=31; todayDate=new Date(); thisday=todayDate.getDay(); thismonth=todayDate.getMonth(); thisdate=todayDate.getDate(); thisyear=todayDate.getYear(); thisyear = thisyear % 100; thisyear = ((thisyear < 50) ? (2000 + thisyear) : (1900 + thisyear)); if (((thisyear % 4 == 0) && !(thisyear % 100 == 0)) ||(thisyear % 400 == 0)) monthdays[1]++; startspaces=thisdate; while (startspaces > 7) startspaces-=7; startspaces = thisday - startspaces + 1; if (startspaces < 0) startspaces+=7; document.write("<table border=2 bgcolor=white "); document.write("bordercolor=black><font color=black>"); document.write("<tr><td colspan=7><center><strong>" + monthnames[thismonth] + " " + thisyear + "</strong></center></font></td></tr>"); document.write("<tr>"); document.write("<td align=center>Su</td>"); document.write("<td align=center>M</td>"); document.write("<td align=center>Tu</td>"); document.write("<td align=center>W</td>"); document.write("<td align=center>Th</td>"); document.write("<td align=center>F</td>"); document.write("<td align=center>Sa</td>"); document.write("</tr>"); document.write("<tr>"); for (s=0;s<startspaces;s++) { document.write("<td> </td>"); } count=1; while (count <= monthdays[thismonth]) { for (b = startspaces;b<7;b++) { linktrue=false; document.write("<td>"); for (c=0;c<linkdays.length;c++) { if (linkdays[c] != null) { if ((linkdays[c][0]==thismonth + 1) && (linkdays[c][1]==count)) { document.write("<a href="" + linkdays[c][2] + "">"); linktrue=true; } } } if (count==thisdate) { document.write("<font color='FF0000'><strong>"); } if (count <= monthdays[thismonth]) { document.write(count); } else { document.write(" "); } if (count==thisdate) { document.write("</strong></font>"); } if (linktrue) document.write("</a>"); document.write("</td>"); count++; } document.write("</tr>"); document.write("<tr>"); startspaces=0; } document.write("</table></p>"); </script> </body> </html>
  25. Checkbox's and php/mysql

    Hello, new here and also sorta new to PHP and all that. I'm trying to find a way to select certain checkboxes and input that data into a database. And then at times go back and view and update the database. The checkboxes may also change at times and I do not want to go add a bunch of fields into the database just to see who's all in it. What I have so far is peoples names in a checkbox form where I can select names and submit it. It then implodes and inserts it into the database like so responders(2,4,47,56) Its not going to be a thing like responders(0,0,1,0) type thing. My format so I do not confuse people responders = row in mysql (numbers) = data in the field in my row Am I way overthinking this, or is there a very simple way to do this? Please note, the name list may update in the future as in names being added, I would like an easy way to add people without killing myself modifying my php/html files. If I lost you in anyway let me know I will see what I can do to make it not confusing.