Insert false data into database [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have multidimentions array hrefs :
Array
(
[1] => Array
(
[0] => http://213b572-ba681bf9cc9e
[1] => http://f057-4139-ac40-bc4449722ffc
[2] => http://b-c151-4ba1-b7b7-842771c36d6b
[3] => http://5a77fb-8fce-4793-868f-c9fd73524037
)
[2] => Array
(
[0] => http://8-d832-4b34-a55b-da04ad8cdd09
[1] => http://b38-6a60-4233-b207-f40fae2ef431
[2] => http://3-f31c-49c4-87ee-fcada05a105f
[3] => http://07514-e438-45e2-906e-b440cbcbf8dc
)
......
[76] => Array
(
[0] => http://8-d832-4b34-a55b-da04ad8cdd09
[1] => http://b38-6a60-4233-b207-f40fae2ef431
[2] => http://3-f31c-49c4-87ee-fcada05a105f
[3] => http://07514-e438-45e2-906e-b440cbcbf8dc
)
When i insert array hrefs above into database
foreach ($hrefs as $id_page => $href) {
foreach ($href as $value) {
mysqli_query($con, "INSERT INTO urls(`id`, `id_page`, `url`)
VALUES ('', '$id_page', '$value')");
}
}
mysqli_close($con);
I want my database are:
| id | id_page | url |
| 1 | 1 | http://jjjjjjjjj |
| 2 | 1 | http://jjjjjjjjj|
| 3 | 1 | http://jjjjjjjjj|
......
| 1000 | 76 | http://jjjjjjjjj|
but result:
| id | id_page | url |
| 1 | 1 | http://jjjjjjjjj |
| 2 | 1 | http://jjjjjjjjj|
| 3 | 1 | http://jjjjjjjjj|
......
| 500 | 35 | http://jjjjjjjjj|
| 501 | 1 | http://jjjjjjjjj|
When insert id_page loop to 35 and return begin 1.
$hrefs is multidimentions array like above. Any resolve?

I would try it this way :
foreach ($hrefs as $id_page => $href) {
foreach ($href as $value) {
mysqli_query($con, "INSERT INTO urls(`id_page`, `url`)
VALUES ('".$id_page."', '".$value."')");
}
}
mysqli_close($con);
Assuming "id" is set to auto_increment in your table.
Edit1: Check your data type in your "urls" table: maybe your "id_page" is too small to hold big values. Set it to int(11) for example.
Edit2: If there is any performance issue, you should execute only one query with multiple values, like that:
$sql = 'INSERT INTO urls(`id_page`, `url`) VALUES ';
foreach ($hrefs as $id_page => $href) {
foreach ($href as $value) {
$sql .= "('".$id_page."','".$value."'),";
}
}
$sql = substr($sql, 0, -1); // delete last comma
mysqli_query($con, $sql);
mysqli_close($con);

Related

Codeigniter Generate Dynamic Table Row Data

I am trying to load dynamic table data, my URI is;
example.coma/admin/view/form/<form_id>
My model query is;
public function getRecords($table, $form_id) {
$this->db->select('*');
$this->db->from($table);
$this->db->where('form_id', $form_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
}
}
This returns an array of data, I need to build a HTML table based on this array.
I'll show an example of two different arrays returned by the query.
Array 1.
(select * from members where form_id = 123)
Array
(
[0] => Array
(
[id] => 104
[member_no] =>
[firstname] => Peter
[lastname] => Keys
[address] => 17 main road
[email] => P3TER#HOTMAIL.CO.UK
[postcode] => UK123
[city] => London
[telnum] => 123123123
[fk_form_submission_id] => 123
)
)
Array 2.
(select * from orders where form_id = 123)
Array
(
[0] => Array
(
[colour] => blue
[type] => shirt
[age] => 34
[size] => medium
[quantity] => 2
[discount] => Y
[posted] => N
)
)
What I want to achieve is display a vertical table to display the dataset. Obviously each table will have different row names, example below;
Table 1.
+---------------+-------+
| ID | 104 |
+---------------+-------+
| Member Number | |
+---------------+-------+
| First Name | Peter |
+---------------+-------+
| Last Name | Keys |
+---------------+-------+
| etc | etc |
+---------------+-------+
Table 2.
+--------+--------+
| Colour | blue |
+--------+--------+
| P Type | shirt |
+--------+--------+
| Age | 34 |
+--------+--------+
| Size | medium |
+--------+--------+
| etc | etc |
+--------+--------+
How can I set these table row names? Do I need to create another array of table headers and merge both arrays?
Any advice is appreciated.
I'm not sure I fully understand - but if you want to set the keys of the array dynamiclly you can do that with foreach loop as:
<table>
<tr><th>Key</th><th>Value</th></tr>
<?php foreach($res[0] as $key => $val)
echo '<tr><td>'. $key . '</td><td>' . $val . '</td></tr>'; ?>
</table>
Edit:
If you want to change the keys name to something more displayable I would recommend using another array for swap (most of the time it done for translation...).
$displayName = array("id" => "ID", "member_no" => "Member Number", "firstname" => "First Name" ..., "type" => "P type", ...);
foreach($res[0] as $key => $val)
echo '<tr><td>'. $displayName[$key] . '</td><td>' . $val . '</td></tr>';
You can also use array_combine but that will need to know which kind of keys you have...
Notice that this solution will work only if the display name are unique for all kind of keys

Select where HSTORE value is $value Laravel

Using the query builder to try and select rows where a value in the HSTORE key $type is present.
My Where Statement:
->where("meta_data_fields->'$type'", 'like', '%'.$query.'%')
Not sure what I'm doing wrong. I get this error:
Undefined column: 7 ERROR: column "meta_data_fields->'Entity::hstore'" does not exist
Any ideas?
to get all records in table that have HSTORE
Model::where("meta_data_fields", 'like', '%'.$query.'%')->get();
This should do normally do the trick. In your case, data in meta_data_fields is a json string. if the search is performed on all rows in the table, will select all. So here is What I did,
I created a queryScope in the model
1 - fetch all data
2 - extract meta_data_field and decode it
3 - loop through meta_data_field
4 - select only that match the criteria and build the array
here is a table
+----+-------------+------------------------------------+-----------+
| id | name | desc | vendor_id |
+----+-------------+------------------------------------+-----------+
| 1 | apples | {"type":"fruit","origin":"mexico"} | 1 |
| 2 | oranges | {"type":"fruit","origin":"peru"} | 1 |
| 3 | Peaches | {"type":"fruit","origin":"mexico"} | 2 |
| 4 | Cherries | {"type":"fruit","origin":"us"} | 1 |
| 5 | banans | {"type":"fruit","origin":"brazil"} | NULL |
| 6 | Water Melon | {"type":"fruit","origin":"mexico"} | 1 |
+----+-------------+------------------------------------+-----------+
public function scopeItems($name ="mexico"){
$items = array();
$data = Self::get();
$meta_fields = json_decode($data -> pluck('desc') ,true);
foreach ($meta_fields as $key => $value) {
if (isset($value['origin']) && $value['origin'] == $name ){
$items[$key] = $data[$key] -> toArray();
}
}
return $items;
}
// output
Array
(
[0] => Array
(
[id] => 1
[name] => apples
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
[2] => Array
(
[id] => 3
[name] => Peaches
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 2
)
[5] => Array
(
[id] => 6
[name] => Water Melon
[desc] => Array
(
[type] => fruit
[origin] => mexico
)
[vendor_id] => 1
)
)
Hope this works for you this time.
So here's what worked in my situation with Laravel.
$result = \DB::select(\DB::raw("SELECT hstore_fields, id from table where lower(hstore_fields->'$type') like lower('%$query%')"));

create a subcategories hierarchy from multidimensional array

I have this table in my DB (I hope it's correctly showed):
+++++++++++++++++++++++++++
| id_child |--| id_parent |
+++++++++++++++++++++++++++
| 5 |--| 2 |
| 6 |--| 2 |
| 7 |--| 4 |
| 8 |--| 4 |
| 9 |--| 5 |
| 10 |--| 5 |
| 11 |--| 9 |
| 12 |--| 9 |
---------------------------
I wrote a php recursive function that create a multidimensional array from a parent passed (in this case '2').
So, if I put a print_r I obtain this result:
Array ( [5] => Array ( [9] => Array ( [11] => Array ( ) [12] => Array ( ) ) [10] => Array ( ) ) [6] => Array ( ) )
How I can obtain a structure of this type?
(I exclude the first parent, 2)
(2)
-5
--9
----11
----12
--10
-6
Thanks.
You would need another recursive function to iterate over your array, like so:
function printCategories($categories, $level = 1) {
foreach ($categories as $cat => $subCats) {
echo str_repeat('-', $level) . $cat . "\n";
printCategories($subCats, $level+1);
}
}
printCategories($categories);
<?php
function printtree($tree, $level) {
$prefix=str_repeat('-',$level);
foreach ($tree as $k=>$v) {
echo "$prefix$k<br>\n";
if (is_array($v)) if (sizeof($v)>0) printtree($v,$level+1);
}
}
$tree=array( 5=>array(9=>array(11=>array(), 12=>array()), 10=>array()), 6=>array());
printtree($tree,1);
?>

Count inverse document frequency (idf) in every documents

I wanna count idf, the formula is IDF=log(D/df) that D is total data and df is many data that contain the searched words.
from the tables :
1. tb_stemming
===========================================================================
|stem_id | stem_before | stem_after | stem_freq | sentence_id |document_id|
===========================================================================
| 1 | Data | Data | 1 | 0 | 1 |
| 2 | Discuss | Discuss | 1 | 1 | 1 |
| 3 | Mining | Min | 1 | 0 | 2 |
===========================================================================
here's the code :
countIDF($total_sentence,$doc_id);
that $total_sentence is
Array ( [0] => 644 [1] => 79 [2] => 264 [3] => 441 [4] => 502 [5] => 18 [6] => 352 [7] => 219 [8] => 219 )
function countIDF($total_sentence, $doc_id) {
foreach ($total_sentence as $doc_id => $total_sentences){
$idf = 0;
$query1 = mysql_query("SELECT document_id, DISTINCT(stem_after) AS unique_token FROM tb_stemming group by stem_after where document_id='$doc_id' ' ");
while ($row = mysql_fetch_array($query)) {
$token = $row['unique_token'];
$doc_id = $row['document_id'];
$ndw = countNDW($token);
$idf = log($total_sentences / $ndw)+1;
$q = mysql_query("INSERT INTO tb_idf VALUES ('','$doc_id','$token','$ndw','$idf') ");
}
}
}
and the function of countNDW is :
function countNDW ($word) {
$query = mysql_query("SELECT stem_after, COUNT( DISTINCT sentence_id ) AS ndw FROM `tb_stemming` WHERE stem_after = '$word' GROUP BY stem_after");
while ($row = mysql_fetch_array($query)) {
$ndw = $row['ndw'];
}
return $ndw;
}
It can't worked well, especially in call from database. All I need is to count in every document_id. How to define it in my code ? please, help me.. thank you so much :)

php Multidimensional array question

So I have an array like the following:
Array
(
[0] => Array
(
[user_id] => 684
[sec_id] => 2
[rank_id] => 1
[rank] => usr
)
[1] => Array
(
[user_id] => 693
[sec_id] => 3
[rank_id] => 5
[rank] => usr
)
)
And I have another array like this
Array
(
[0] => 2
[1] => 7
[2] => 27
)
I want the value of the second array to be added at the end of each arrays of the 1st array, and it should be multiplied. I mean, if I have 100 arrays in the first array, and 3 elements in the second array, I should have 300 in the resulting array.
Taking example of the above, I would like to have something as follows:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 2
684 | 2 | 1 | usr | 7
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 2
693 | 3 | 5 | usr | 7
693 | 3 | 5 | usr | 27
I tried with the following function, but it's not working.
function getR($arr_one,$arr_two) {
foreach ($arr_one as $k=>&$v) {
foreach ($arr_two as $x=>&$y) { $v['menu_id'] = $y; }
}
return $arr_one;
}
This is just making an array like this:
user_id | sec_id | rank_id | rank | menu_id
684 | 2 | 1 | usr | 27
693 | 3 | 5 | usr | 27
Means, it's just adding menu_id at the end of each element of the first array, but not multiplying. Any idea, I'm surely missing something.
Thanks guys.
function getR($arr_one,$arr_two) {
$new_arr = array();
foreach ($arr_one as $k=>$v) {
foreach ($arr_two as $x=>$y) {
$this_item = $v;
$this_item['menu_id'] = $y;
$new_arr[] = $this_item;
}
}
return $new_arr;
}
I'm not going to ask... but try this:
<?php
function crazy ($arr1,$arr2) {
foreach ($arr1 as $key=>$value) {
foreach ($arr2 as $value2) {
$nvalue=$value;
$nvalue[]=$value2;
$new[]=$nvalue;
}
}
return $new;
}
$arr1=array(array('user'=>1,'dude'=>2),array('user'=>2,'dude'=>3));
$arr2=array(2,7,27);
print_r(crazy($arr1,$arr2));
this is tested too, http://www.ideone.com/Of126
Without testing (eek!) I imagine something like this:
function getR( $arr_one, $arr_two )
{
$second_key = 0;
foreach ( $arr_one as $k => &$v )
{
$v['menu_id'] = $second_key++;
if ( 3 == $second_key ) $second_key = 0;
}
return $arr;
}
Presumably, you're passing the first array by reference? Not sure what $arr is that you're returning though...

Categories