dimanche 28 juin 2015

Give array elements unique IDs

I am trying to multiply every element in an array by the next 12 elements:

array.each do |n|
    a = array.index(n)
    b = a + 12
    product = 1
    array[a..b].each { |i| product *= i }
    highest = product if product > highest
end

I run into a problem when there are multiple occurrences of the same integer in the array:

[1, 2, 3, 7, 5, 4, 7] # this is not the actual array

When the second 7 runs through my block, its array.index(n) becomes 3 (the index of the first 7) when I want it to be 6 (the index of the particular 7 I am working with). I'm pretty sure this can be solved by giving each element of the array a unique 'id', but I'm not sure how I would go about doing this.

My question is, how do I give every element in an array a unique id? The Array#uniq method is not what I am looking for.

mysqli_real_escape_string foreach function db_array_update

Someone wrote a PHP program many times ago for me, and now I got this error when I run the code :

mysqli_real_escape_string() expects parameter 2 to be string, array given in....

I cannot fix this here is the code :

function db_array_update($table, $a, $where) 
{    
    $q = "update $table set ";
    $b = NULL;  

    foreach($a as $key => $value)   
    {   
        if (is_int($key))
            continue;   

        $con = mysqli_connect("localhost", MYSQLUSER , MYSQLPASS, MYSQLDB);
        $b[] = "$key='".mysqli_real_escape_string($con, $value)."'";        
    }

    $q .= implode(",", $b);
    $q .= " where ".$where;

    db_query($q);

}

and I use it like this :

db_array_update("all_data",array('last_fetched' =>date("Y/m/d H:i:s"),'name'=>$name, 'creation'=>$creat, 'expiration' =>$expire,"id=".$res['id']);

Can someone help me how should I fix this ? Tried many things but not work...

How to use Arrays, with Strings

I'm a C++ programmer, who's still in the nest, and not yet found my wings. I was writing a Calendar program, and I discovered, that C++ does not support a string type. How do I make an Array, that will be able to store strings of characters?

I've thought of creating an enumerated data type, as the array type. While, it will work, for my Calendar, it won't work if say I was creating a database of the names of students in my class.

http://ift.tt/1NnnPZv I got; "error, 'string' does not name a type."

Input EditText(Type:Number) into array[]

I have an EditText (input type:numberDecimal),that i want to keep adding values with save them to an Array. how can i do this ?
for an example:

EditText:"1234"
Array[0]=1;
Array[1]=2;
Array[2]=3;
Array[3]=4;

Thanks...

Java Standard Search Joptionpane

im learning Java and encounter some problem. Tried many hours but still cant solve. Pls bear with me. Thank you all.

How can i do a search result like shown in enter image description here

and when user press enter, will go next search result? Sori for pasting all my code here. Thanks all.

    import javax.swing.JOptionPane;

public class Makan {

static int[] count;
static String[] foodName;
static String[] country;
static String[] type;
static String[] description;
static String[] price;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    count = new int[7];
    count[0] = 1;
    count[1] = 2;
    count[2] = 3;
    count[3] = 4;
    count[4] = 5;
    count[5] = 6;
    count[6] = 7;

    foodName = new String[7];
    foodName[0] = "Fish and Chips";
    foodName[1] = "Chicken Cutlet";
    foodName[2] = "Oyako Don";
    foodName[3] = "Nasi Goreng";
    foodName[4] = "Seafood Hor Fun";
    foodName[5] = "Ice Kachang";
    foodName[6] = "Ice cream Waffles";

    country = new String[7];
    country[0] = "Western";
    country[1] = "Western";
    country[2] = "Asian";
    country[3] = "Asian";
    country[4] = "Asian";
    country[5] = "Asian";
    country[6] = "Asian";

    type = new String[7];
    type[0] = "Main";
    type[1] = "Main";
    type[2] = "Main";
    type[3] = "Main";
    type[4] = "Main";
    type[5] = "Dessert";
    type[6] = "Dessert";

    description = new String[7];
    description[0] = "Fish fillets in tartar sauce, served with fried chips";
    description[1] = "Breaded chicken cutlet with melted cheese, served with potato and fries";
    description[2] = "Chicken in Japanese rice, served with a bowl of Miso Soup";
    description[3] = "Fried Rice with Satay";
    description[4] = "Flat rice noodles in a tasty gravy with prawns, fish cakes and sliced fish.";
    description[5] = "Shaved ice dessert with jelly, red beans, sweet corn and palm seeds, topped with sweet syrup";
    description[6] = "Freshly baked waffles topped with 2 scoops icecream of your choice";

    price = new String[7];
    price[0] = "16.80";
    price[1] = "15.90";
    price[2] = "13.90";
    price[3] = "10.20";
    price[4] = "10.50";
    price[5] = "5.60";
    price[6] = "8.20";

    String menu = "Main Menu\n";
    menu += "==================\n";
    menu += "1. Display all Menu Items\n";
    menu += "2. Display Menu Item names for selection\n";
    menu += "3. Search Food Type\n";
    menu += "0. Quit\n";
    menu += "==================";

    String input;
    int optionSelected;
    do {

        input = JOptionPane.showInputDialog(menu);
        optionSelected = Integer.parseInt(input);
        System.out.println(optionSelected);

        if (optionSelected == 1) {

            displayAllMenuItems();

        } else if (optionSelected == 2) {

            showAllSelections();
        }

        else if (optionSelected == 3) {
            searchBox();
        }

        else if (optionSelected == 0) {
            System.exit(0);

        }

    } while (optionSelected != 0);
}

// handle option 1...
public static void displayAllMenuItems() {

    String hotkey = "";
    hotkey += "==================\n";
    hotkey += "Enter N for next menu item\n";
    hotkey += "Enter P for previous menu item\n";
    hotkey += "Enter M to return main menu\n";

    int index = 0;
    String input = "";
    do {
        String menu = "Menu Item " + count[index] + " of " + count.length;
        menu += "\n================\n";
        menu += "Name: " + foodName[index] + "\nCountry: " + country[index]
                + "\nType: " + type[index] + "\nDescription: "
                + description[index] + "\nPrice: " + price[index] + "\n";
        menu += hotkey;

        input = JOptionPane.showInputDialog(menu);

        if (input.equalsIgnoreCase("n")) {

            index++;

        }

        else if (input.equalsIgnoreCase("p")) {

            index--;

        }

    } while (!input.equalsIgnoreCase("M"));

}

// handle option 2
public static void showAllSelections() {
    int index;
    String input = "";
    String menu = "";
    menu += "Menu Item Selection Menu\n";
    menu += "==================\n";

    for (index = 0; index < foodName.length; index++) {

        menu += index + 1 + ". " + (foodName[index]) + "\n";

    }
    menu += "Please enter your selection\n";

    input = JOptionPane.showInputDialog(menu);
    int optionSelected;
    optionSelected = Integer.parseInt(input);
    listSpecificItems(optionSelected);

}

// list specific items
public static String listSpecificItems(int index) {
    String menu = "";
    String input = "";

    menu = "Menu Item " + count[index - 1] + " of " + count.length;
    menu += "\n================\n";
    menu += "Name: " + (foodName[index - 1]) + "\nCountry: "
            + (country[index - 1]) + "\nType: " + (type[index - 1])
            + "\nDescription: " + (description[index - 1]) + "\nPrice: "
            + (price[index - 1]) + "\n";
    menu += "================\n";
    menu += "Enter M to return to main menu\n";

    if (input.equalsIgnoreCase("m")) {

        displayAllMenuItems();
    }
    input = JOptionPane.showInputDialog(menu);

    return input;
}


// Handle Search Box
public static void searchBox() {
    String menu = "";
    String input = "";

    menu = "Search By\n";
    menu += "================\n";

    input = JOptionPane.showInputDialog(menu);
    showSearchResult(input);
}

/** stuck here how define search n show results **/
public static String showSearchResult(String input) {


}

}

Updating nested arrays with Laravel

First sorry, i am only developing in php for a month and i do not know how to update this with laravel

I would like to update multiple photos.

Photos have a title description, etc.

My problem is i have no clue how to do it.

I tried the following

public function update(Request $request, Photo $photo)
    {
         // loop throug array of id's
         foreach ($request->photo_id as $id) 
         {
            // find 
             $photos = $photo->find($id);

             // loop throug input fields
             foreach ($request->except('_token', 'tags', 'photo_id') as $key => $value) 
             {
                $photos->$key = $value
                $photos->save();
             }

         }

         die();
    }   

I get the following error

preg_replace(): Parameter mismatch, pattern is a string while replacement is an array

So i figured out the problem is with the value

And the results are like this

Key variable

string(5) "title"
string(10) "country_id"
string(7) "city_id"
string(11) "category_id"
string(9) "cruise_id"
string(12) "itinerary_id"
string(4) "desc"
string(6) "people"
string(5) "title"
string(10) "country_id"
string(7) "city_id"
string(11) "category_id"
string(9) "cruise_id"
string(12) "itinerary_id"
string(4) "desc"
string(6) "people"

Value variable results

array(2) {
  [0]=>
  string(9) "title one"
  [1]=>
  string(9) "title two"
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "1"
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "1"
}
array(2) {
  [0]=>
  string(0) ""
  [1]=>
  string(0) ""
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(0) ""
}
array(2) {
  [0]=>
  string(1) "1"
  [1]=>
  string(0) ""
}

I tried several other attempts but nothing works

Could please someone help me out with this?

Can't figure out this array [duplicate]

This question already has an answer here:

I have an array:

Array (
    [variants] => Array ( 
        [0] => Array (
            [barcode] => 610373772697
            [compare_at_price] =>
            [created_at] => 2015-06-27T01:56:42-07:00
            [fulfillment_service] => manual
            [grams] => 190
            [id] => 3856358467
            [inventory_management] => shopify
            [inventory_policy] => deny
            [option1] => Default Title
            [option2] =>
            [option3] =>
            [position] => 1
            [price] => 20.99
            [product_id] => 1259972867
            [requires_shipping] => 1
            [sku] => 1125442
            [taxable] => 1
            [title] => Default Title
            [updated_at] => 2015-06-27T01:56:42-07:00
            [inventory_quantity] => 100
            [old_inventory_quantity] => 100
            [image_id] =>
            [weight] => 0.42
            [weight_unit] => lb
        ) 
        [1] => Array (
            [barcode] => 364031530906
            [compare_at_price] =>
            [created_at] => 2015-06-27T01:56:42-07:00
            [fulfillment_service] => manual
            [grams] => 131
            [id] => 3856359043
            [inventory_management] => shopify
            [inventory_policy] => deny
            [option1] => Default Title
            [option2] =>
            [option3] =>
            [position] => 1
            [price] => 17.24
            [product_id] => 1259973059
            [requires_shipping] => 1
            [sku] => 0116350
            [taxable] => 1
            [title] => Default Title
            [updated_at] => 2015-06-27T01:56:42-07:00
            [inventory_quantity] => 100
            [old_inventory_quantity] => 100
            [image_id] =>
            [weight] => 0.29
            [weight_unit] => lb

        ) 
        [2] => Array (
            [barcode] => 364031534003
            [compare_at_price] =>
            [created_at] => 2015-06-27T01:56:43-07:00
            [fulfillment_service] => manual
            [grams] => 390
            [id] => 3856359107
            [inventory_management] => shopify
            [inventory_policy] => deny
            [option1] => Default Title
            [option2] =>
            [option3] =>
            [position] => 1
            [price] => 27.74
            [product_id] => 1259973123
            [requires_shipping] => 1
            [sku] => 0116368
            [taxable] => 1
            [title] => Default Title
            [updated_at] => 2015-06-27T01:56:43-07:00
            [inventory_quantity] => 100
            [old_inventory_quantity] => 100
            [image_id] =>
            [weight] => 0.86
            [weight_unit] => lb
        )
    )
) 
) 

And no matter how many For Loops I try, I cannot figure out how to extract the [id] and the [product_id] and pass them to variables using PHP.

My goal is to dump the id and product_id into a mysql database and I once I get the values out I can do that easily.

Can anyone help guide me?