mardi 4 août 2015

Append/delete items from array in php/html file

I am a PHP beginner and need some help.

I have a config.data file that has the $logins array serialized in it. (thanks @Barmar!) I have signin.php (below) which I would like to modify in the following way:

  1. Add a 'add project' button which would append the info to the $logins array
  2. Add a 'delete project' button which would delete the info to the $logins array.

Any help to accomplish these two goals would be appreciated!

config.data:

$logins = array(
'project1' =>
        array(
        'password' => 'mypassword',
        'title' => 'Capacitors Project Planner',
        'emails' => array('myweb@mywebsite.com')
         ),
'project2' =>
    array(
        'password' => 'mypassword2',
        'title' => 'My second project',
        'emails' => array('manager@youwebsite.com',
                        'worker@youwebsite.com')
        )
);

signin.php:

require_once( 'config.php' );
//displays arrays
<table class="table" border='1'>
<thead>
    <tr>
    <th> Project name</th>
    <th> Project Login</th>
    </tr>
    </thead>
<tbody>
<?php foreach( $logins as $projname => $projinfos ): ?>
    <tr>
        <td><?= $projname ?></td>
        <td><?= $projinfos["title"]?>
        </td>
        </tr>

<?php endforeach; ?>
</tbody>
</table>



via Chebli Mohamed

CI update_batch; Concatenate and increment variable and string as array value

Using CI update_batch(), I am unable to set a value where I increment an integer db value. This works successfully with CI set(); update(), but batch is the better option as there are multiple updates.

I have columns that all have the same characters at the end of the name, with different characters at the beginning (different years): 2014x , 2015x , 2016x , etc. So I have created a $var that identifies the year, then I add the string 'x' and 'y' via concatenation. Finally, the value setting in the array is to increment by 1 so I add +1 . This concatenation works fine in the keys - that is to say I am updating the correct column and fields.

$data = array(
    array('name' => $name1,
            $var.'x' => $var.'x+1'),
    array('name' => $name2,
            $var.'y' => $var.'y+1')
            );          
$this->db->update_batch('my_table', $data, 'tname');

In the above case, the fields are updated with the value of only $var - the year that has been defined.

I have also tried the following:

=> '{"$var.x+1"}'  // places a '0' value in the field
=> $var.'x' +1       // places the value of $var
=> '$var.x+1'        // places a '0' value in the field

How can I use update_batch() to increment my field by 1?



via Chebli Mohamed

swift: "for" loop and "if" statement return four arrays with one element instead of one array with four elements

I have an array of NSDate. I am filtering this array with a timeIntervalSinceDate to only keep specific ones. After looping through the array, and submitting an "if" statement to compare the elements in the array with others, I intend to append the desired ones into a new array.

However, after the whole process, it returns an array of one element every time, returning all the right elements, but in separate arrays every time. Instead of having one array of four elements, I end up with four arrays of one.

Here are the codes:

  var query = PFUser.query()

query!.whereKey("username", equalTo: PFUser.currentUser()!.username!)

query!.findObjectsInBackgroundWithBlock { (objects, NSError) -> Void in

if let objects = objects as? [PFObject] {

        for member in objects {

          if member["Network"] != nil {

                var acceptedMembers: [String] = member["Network"] as! [String]

               self.usernames = acceptedMembers


  var query = PFUser.query()

  query.findObjectsInBackgroundWithBlock({ (objects2, error2) -> Void in

  if let objects2 = objects2 as? [PFUser] {

                        for otherUser in objects2 {

   if contains(self.usernames, otherUser.username!) {

                           self.nameMember = otherUser.username!

                            self.setName()


                            var otherUserElement: AnyObject? = otherUser["myElement"]

                                    if otherUserElement != nil {

                                var OUEL = otherUserElement as! NSArray


                                var currentUserElement: AnyObject? = PFUser.currentUser()!["myElement"]

                                         if currentUserElement != nil {

                                var CUEL = currentUserElement as! NSArray

                                 var count = CUEL.count




                                   for point1 in OUEL {

                                        var arrayPoint1 = point1 as! NSArray

                                        var elem1: AnyObject = arrayPoint1[0]

                                        var elem11: AnyObject = arrayPoint1[1]

                                        var time1: AnyObject = arrayPoint1[2]


                                        self.element11 = elem1 as! Double
                                        self.element111 = elem11 as! Double
                                        self.date1 = time1 as! NSDate



                                        for point2 in CUEL {

                                            var arrayPoint2 = point2 as! NSArray

                                            var elem2: AnyObject = arrayPoint2[0]

                                            var elem22: AnyObject = arrayPoint2[1]

                                            var time2: AnyObject = arrayPoint2[2]


                                            self.element22 = lat2 as! Double
                                            self.element222 = long2 as! Double
                                            self.date2 = time2 as! NSDate

                                            self.tryPoint = point2 as! [AnyObject]




                                          self.Y.append(self.tryPoint)


                                            let compo = self.cal.components(.CalendarUnitSecond | .CalendarUnitMinute | .CalendarUnitHour | .CalendarUnitDay | .CalendarUnitMonth, fromDate: self.date2)


                                            self.second = compo.second

                     let unit2:NSCalendarUnit = .CalendarUnitSecond

                                            let components = self.cal.components(unit2, fromDate: self.date1, toDate: self.date2, options: nil)

      var range = -15...15

      if (self.element11 == self.element22) && (self.element111 == self.element222) && contains(range, components.second) {

      self.dateX.append(self.date2)

      var countArray = [NSDate]()

     if NSDate().timeIntervalSinceDate(self.date2) <= 86400 {

                                                for n in self.dateX {

                                                    if n == self.date2 {

                                                     countArray.append(n)

                                                    } } }



} } }}}}}}   })
}}}}

Do you have any idea of what might cause this issue ? Why would it return different arrays for each element every time instead of one array with all ?

Thank you very much,



via Chebli Mohamed

compute the wight of each tag for each item in matlab

I have an array like below:

<table style="width:10%">
  <tr>
    <td>Item</td>
    <td>Tag</td> 
  </tr>
  <tr>
    <td>1</td>
    <td>2</td> 
  </tr>
  <tr>
    <td>1</td>
    <td>3</td> 
  </tr>
  <tr>
    <td>2</td>
    <td>1</td> 
  </tr>
  <tr>
    <td>3</td>
    <td>2</td> 
  </tr>
  <tr>
    <td>3</td>
    <td>2</td> 
  </tr>
  <tr>
    <td>3</td>
    <td>4</td> 
  </tr>
</table>

I want to compute the weight of each tag for each item in array. for example the wight of tag 2 for item 3 is 0.66, while the wight of tag 2 for item 1 is 0.5. Is there a simple way in matlab to do this?



via Chebli Mohamed

Accessing an ArrayList method, when the ArrayList is inside an object

I am trying to design a text based game.

The game will have a feature in which, if the user inputs get all, all the objects in the room must be added to the player's inventory.

The inventory of the player is an ArrayList of objects and so is the room's object "sack"

The code that i have written is as follows:

public void getAll(Room room, Player player)
  {
      for(int i = 0; i < room.objectNames.size(); i ++)
      {
          player.inventoryObjects.add(room.objects.get(i));
      }
  }

This method is inside the main class, i.e , the class which has the main method.

The arraylists are declared as:

public ArrayList<Objects> inventoryObjects = new ArrayList<Objects>();
//this is in the Player Class

public ArrayList<Objects> objects = new ArrayList<Objects>();
//this is in the Room Class

Can you tell me an alternative to line: 3 in the first piece of code that i have typed(the one with the loop) ?

-Thanks in advance



via Chebli Mohamed

JSON to Array react, jquery

I have json data obtained form my server and i want to put in a list, i already read Convert JSON To Array Javascript and Converting data from JSON to JavaScript array but i can't figure out what of all the options can i use in my code my json is like this:

{"response": {"user": [{"username": "", "description": "", "picture_url": "", "name": "", "phone": ""}]}, "success": true}

and i want to put it on an array like this:

[{username: "",description: "", picture_url: "", name: "", phone: ""}]

what can i do for this? i already try the answers of this: Converting JSON Object into Javascript array

i also try some functions like this:

$.each(data, function(i,obj){
                $(arr).append(obj.username+'</br>');
                $(arr).append(obj.description+'</br>');
                $(arr).append(obj.name+'</br>');
            });

and this:

var results = data;
            var arr = [], item;
            for (var i = 0, len = results.length; i < len; i++) {
                item = results[i];
                arr.push({username: item.username, description: item.description, name: item.name});
            }

but with the second one dont show anything and dont show any error, with the first one, showme an unespecthed error arr is not defined.



via Chebli Mohamed

Need to Display the Results of MySQLi num_rows in Separate Table from Array Results

I have a page that displays a MySQLi query result array. On this page the data comes to the page correctly but for my $result['rows'] which is the result of num_rows of the array results are showing up for each record and then the array results are being displayed. For example if there are 100 records to the query there is '100' displayed 100 times before the array results. I know it's because I'm using 'foreach' but I can't get it work with anything else. I've tried 'while', 'for', and 'continue' but nothing works but 'foreach' to retrieve the data correctly. What am I missing?

Here is the code:

<?php
            if($results) {
    echo "<table id='test' class='tablesorter' border='2'>";
    echo "<thead>";
    echo "<tr>";
    echo "<th class='header'># of Records</th>";
    echo "</tr>";
    echo "</thead>";
    echo "<tbody>";

        foreach($_SESSION['results'] as $result) {

                echo "<tr>";
                echo "<td>{$result['rows']}</td>";
                echo "</tr>";

            }
            echo "</tbody>";
            echo "</table>";
            }
            ?>

            <?php
            if($results) {
    echo "<table id='test' class='tablesorter' border='2'>";
    echo "<thead>";
    echo "<tr>";
    echo "<th class='header'>Date Set</th>";
    echo "<th class='header'>Branch</th>";
    echo "<th class='header'>Appointment Date</th>";
    echo "<th class='header'>Employee</th>";
    echo "<th class='header'>First Name</th>";
    echo "<th class='header'>Last Name</th>";
    echo "<th class='header'>Last Four</th>";
    echo "<th class='header'>Phone</th>";
    echo "<th class='header'>City</th>";
    echo "<th class='header'>State</th>";
    echo "<th class='header'>Zip</th>";
    echo "</tr>";
    echo "</thead>";
    echo "<tbody>";


            foreach($_SESSION['results'] as $result) {

                echo "<tr>";
                echo "<td>{$result['set_date']}</td>";
                echo "<td>{$result['branch']}</td>";
                echo "<td>{$result['appt_date']}</td>";
                echo "<td>{$result['employee']}</td>";
                echo "<td>{$result['fname']}</td>";
                echo "<td>{$result['lname']}</td>";
                echo "<td>{$result['last_four']}</td>";
                echo "<td>{$result['phone']}</td>";
                echo "<td>{$result['city']}</td>";
                echo "<td>{$result['state']}</td>";
                echo "<td>{$result['zip']}</td>";
                echo "</tr>";

            }
            echo "</tbody>";
            echo "</table>";            

}else{

    echo "No Records Found";
}
?>



via Chebli Mohamed