PHP Function to search values in database and replace in a loop - php

I am building a web scraper in PHP and I am not so experimented with all this stuff. What I am trying to achieve is as following:
Split an array of values into strings using foreach
Search any value in a predefined MYSQL table. If value is identical with one of the defined ones, it should be replaced. Otherwise it should remain the same
Put the new values back into an array
Below is my snippet. Basic structure of database is "ID, Marime, Inlocuire". "Marime" is the column to search on, and "Inlocuire" is the column to replace value with.
foreach ($marimi as $marime) {
$sizes[]=trim(strtok($marime->innertext, '-'));
$newArray = array_filter($sizes, 'myFilter');
foreach ($newArray as $marimeFixa) {
$marimeDefinita = $conn->query("SELECT * FROM oc_1_tabelmarimi WHERE Marime = '$marimeFixa'");
if($marimeDefinita->num_rows == 0) {
$marimeFixa = $marimeFixa;
} else {
$marimeFixa = $marimeDefinita['Inlocuire'];
}
$arrayMarimi[] = $marimeFixa;
}
print_r($arrayMarimi);
}
However this doesn't seem to work. Any help is greatly appreciated. Thanks!

try:
$marimeDefinita = $marimeDefinita->fetch_assoc();
before if($marimeDefinita->num_rows == 0) {
or
$marimeFixa = $marimeDefinita->Inlocuire;

Related

How do I place in order 3,4 and 5 values based on amount (high-low) php

I started writing this code to determine which value within 3 date of births was the highest. As I got to as far as you can see I realised this was a silly way of doing it. There will at times be 4 values or even 5 values so writing every combination like this is sloppy.
$new_date_year1 etc are pre defined from a html form.
Can someone recommend another way of doing this.
$valuename1 = "Tom";
$valuename2 = "Jack";
$valuename3 = "Fred";
if ($amount == "3") {
if ($new_date_year1 > $new_date_year2 and $new_date_year1 > $new_date_year3 and $new_date_year2 > $new_date_year3) {
$highest_amount = $valuename1;
$second_amount = $valuename2;
$third_amount = $valuename3;
}
else if ($new_date_year1 > $new_date_year2 and $new_date_year1 > $new_date_year3 and $new_date_year3 > $new_date_year2) {
$highest_amount = $valuename1;
$second_amount = $valuename3;
$third_amount = $valuename2;
}
}
Thanks is advance for any help
just in html form replace variable names from $new_date_year1 into $new_date_year[] etc.
then in php You will have array that is sortable, so:
$new_date_year[] = '2015';
$new_date_year[] = '2018';
$new_date_year[] = '2016';
You can insert data to the array in this way as well(using array_push):
// implementing an empty array.
$new_date_year = [];
//using array_push
//you can pass multiple values to the array for explain it further i will pass '2015','2016','2018' to the array.
array_push($new_date_year,'2015','2016','2018');
After adding the values to the array you can sort the array using rsort which ,
sorts an array in reverse order (highest to lowest).
rsort($new_date_year);
$highest_amount = $new_date_year[0];
$second_amount = $new_date_year[1];
$third_amount = $new_date_year[2];

Finding a faster function then "gethostbyname"?

I run a script under xampp with a mysqlDB.
I check if a domainname has an ip.
The problem is, that I have to check over 100000 domain names from a MySQL_DB.
The function "gethostbyname" works great, but my solution is too slow.
while($row = mysqli_fetch_array($db_res)) { // get the DB domainnames entrys
if (empty($row['status'])) {
$items[] = $row['domainnames'];
}
foreach ($items AS $domain) {
if ( gethostbyname($domain) != $domain ) {
do somthing.....
}
}
}
How do I get it faster?
Your foreach() loop inside of your while() loop is simply a bad idea. Think about it.
As you iterate the result set, $items swells and swells -- this means that the foreach() will have to work longer and longer and longer.
Ultimately, if you need to process the gethostbyname() value for the next task in your script, you should be storing that value at the same time that you INSERT the entry into your table the first time -- perhaps the new column can be host.
The smart money is not to call gethostbyname() 100000 times; have the value ready when you SELECT it.
Beyond the above logic, I don't see the need to declare an array with a single element/string, then iterate it.
In fact, your query should contain a WHERE clause that excludes rows that have a null/0/blank status value AND includes rows that have a host (new column) value that matches $domain so that php doesn't have to bother any qualifying/disqualifying conditions.
foreach ($db_res as $row) { // yes, you can simply iterate the result object
// do whatever with the associative string elements (e.g. $row['domainnames'])
// ...you know this is a string and not an array, right? -^^^^^^^^^^^^^^^^^^^
}
thanks for the answers.
With your assistance i was able to reduce the procedure to:
while($row = mysqli_fetch_array($db_res))
{
$domain = $row['domainnames'];
if ( gethostbyname($domain) != $domain ) {
do somthing.....;
}
else{
do somthing.....;
}
}
it feels a little bit faster but not enough.
#mickmackusa i catch now only the empty "status" fields:
$db_res = mysqli_query ($db_link, "select domainnames FROM domaintable WHERE status = ''")
Looks like when your while loop iterates, it uses the $items from the last iteration - which will waste time - so please try this version (putting the foreach into the if:
while($row = mysqli_fetch_array($db_res)) { // get the DB domainnames entrys
if (empty($row['status'])) {
$items[] = $row['domainnames'];
foreach ($items AS $domain) {
if ( gethostbyname($domain) != $domain ) {
do somthing.....
}
}
}
}

Extract CSV values from an array and display them Individually as a List

I have an array of CSV values in my Database like category1[obj1,obj2,obj3..]
View
<?php echo $row_company->category1;?>
Controller
$row_company = $this->employers_model->get_company_details_by_slug($company_name);
The employers_model then calls a procedure which in turn executes the required query which displays the contents of row = category1 in this fashion,
obj1,obj2,obj3...
I want to be able to show this result without the commas and as a list like this
obj1
obj2
obj3..
I'm Using CodeIgniter MVC Framework, I have some vague idea about the uses of implode function, and came across preg_split too, But don't know where to start meddling around from, the view or the controller.
Any direction towards the solution would be appreciated.
Edit : row_company in detail.
$row_company = $this->employers_model->get_company_details_by_slug($company_name);
if(!$row_company){
redirect(base_url(),'');
exit;
}
$company_website = ($row_company->company_website!='')?validate_company_url($row_company->company_website):'';
$data['row_company'] = $row_company;
$data['company_logo'] = $company_logo;
$data['company_join'] = $company_join;
$data['company_website'] = $company_website;
$data['company_location'] = $company_location;
$data['title'] = $row_company->company_name.' jobs in '.$row_company->city;
$this->load->view('company_view',$data);
}
employers_model content
public function get_company_details_by_slug($slug) {
$Q = $this->db->query('CALL get_company_by_slug("'.$slug.'")');
if ($Q->num_rows > 0) {
$return = $Q->row();
} else {
$return = 0;
}
$Q->next_result();
$Q->free_result();
return $return;
}
the procedure itself get_company_details_by_slug
BEGIN
SELECT emp.ID AS empID, emp.sts1, pc.ID, emp.country, emp.city, pc.company_name, pc.company_description, pc.company_location, pc.company_website, pc.no_of_employees, pc.established_in, pc.company_logo, pc.company_slug, pc.category1, pc.category2, pc.category3, pc.company_join
FROM `pp_employers` AS emp
INNER JOIN pp_companies AS pc
WHERE pc.company_slug=slug AND emp.sts1 ='active';END
Figured it out after referring many codeigniter/php forums.
Pretty simple actually,
<?php $array = explode(',', $row_company->category1);
foreach ($array as $item)
{
echo "<li>$item</li>";
}
?>
Thank you Nigel for pointing out to use explode function.
Cheers

Inserting a variable with multiple values into a mysql database

I thought I would edit my question as by the comment it seems this is a very insecure way of doing what I am trying to acheive.
What I want to do is allow the user to import a .csv file but I want them to be able to set the fields they import.
Is there a way of doing this apart from the way I tried to demonstrate in my original question?
Thank you
Daniel
This problem I am having has been driving me mad for weeks now, everything I try that to me should work fails.
Basically I have a database with a bunch of fields in.
In one of my pages I have the following code
$result = mysql_query("SHOW FIELDS FROM my_database.products");
while ($row = mysql_fetch_array($result)) {
$field = $row['Field'];
if ($field == 'product_id' || $field == 'product_name' || $field == 'product_description' || $field == 'product_slug' || $field == 'product_layout') {
} else {
echo '<label class="label_small">'.$field.'</label>
<input type="text" name="'.$field.'" id="input_text_small" />';
}
}
This then echos a list of fields that have the label of the database fields and also includes the database field in the name of the text box.
I then post the results with the following code
$result = mysql_query("SHOW FIELDS FROM affilifeed_1000.products");
$i = 0;
while ($row = mysql_fetch_array($result)) {
$field = $row['Field'];
if ($field == 'product_name' || $field == 'product_description' || $field == 'product_slug' || $field == 'product_layout') {
} else {
$input_field = $field;
$output_field = mysql_real_escape_string($_POST[''.$field.'']);
}
if ($errorcount == 0) {
$insert = "INSERT INTO my_database.products ($input_field)
VALUES ('$output_field')";
$result_insert = mysql_query($insert) or die ("<br>Error in database<b> ".mysql_error()."</b><br>$result_insert");
}
}
if ($result_insert) {
echo '<div class="notification_success">Well done you have sucessfully created your product, you can view it by clicking here</div>';
} else {
echo '<div class="notification_fail">There was a problem creating your product, please try again later...</div>';
}
It posts sucessfully but the problem is that it creates a new "row" for every insert.
For example in row 1 it will post the first value and then the rest will be empty, in row 2 it will post the second value but the rest will be empty, row 3 the third value and so on...
I have tried many many many things to get this working and have researched the foreach loop which I haven't been familiar with before, binding the variable, imploding, exploding but none of them seem to do the trick.
I can kind of understand why it is doing it as it is wrapped in the while loop but if I put it outside of this it only inserts the last value.
Can anyone shed any light as to why this is happening?
If you need any more info please let me know.
Thank you
Daniel
You're treating each field you're displaying as its own record to be inserted. Since you're trying to create a SINGLE record with MULTIPLE fields, you need to build the query dynamically, e.g.
foreach ($_POST as $key => $value);
$fields[] = mysql_real_escape_string($key);
$values[] = "'" . msyql_real_escape_string($value) . "'";
} // build arrays of the form's field/value pairs
$field_str = implode(',', $fields); // turn those arrays into comma-separated strings
$values_str = implode(',', $values);
$sql = "INSERT INTO yourtable ($field_str) VALUES ($value_str);"
// insert those strings into the query
$result = mysql_query($sql) or die(mysql_error());
which will give you
INSERT INTO youtable (field1, field2, ...) VALUES ('value1', 'value2', ...)
Note that I'm using the mysql library here, but you should avoid it. It's deprecated and obsolete. Consider switching to PDO or mysqli before you build any more code that could be totally useless in short order.
On a security basis, you should not be passing the field values directly through the database. Consider the case where you might be doing a user permissions management system. You probably wouldn't want to expose a "is_superuser" field, but your form would allow anyone to give themselves superuser privileges by hacking up their html form and putting a new field saying is_superuser=yes.
This kind of code is downright dangerous, and you should not be using it in a production system, no matter how much sql injection protect you build into it.
Alright....I can't say that I know exactly whats going on but lets try this...
First off....
$result = mysql_query("SHOW FIELDS FROM my_database.products");
$hideArray = array("product_id","product_name","product_description", "product_slug","product_layout");
while ($row = mysql_fetch_array($result)) {
if (!in_array($row['Field'], $hideArray)){
echo '<label class="label_small">'.$field.'</label>
<input type="text" name="'.$field.'" id="input_text_small" />';
}
}
Now, why you would want to post this data makes not sense to me but I am going to ignore that.....whats really strange is you aren't even using the post data...maybe I'm not getting something....I would recommend using a db wrapper class...that way you can just through the post var into....ie. $db->insert($_POST) ....but if you ware doing it long way...
$fields = "";
$values = "";
$query = "INSERT INTO table ";
foreach ($_POST as $key => $data){
$values .= $data.",";
$fields .= $fields.",";
}
substr($values, 0, -1);
substr($fields, 0, -1);
$query .= "(".$fields.") VALUES (".$values.");";
This is untested....you can also look into http://php.net/manual/en/function.implode.php so you don't have to do the loop.
Basically you don't seem to understand what is going on in your script...if you echo the sql statements and you can a better idea of whats going....learn what is happening with your code and then try to understand what the correct approach is. Don't just copy and paste my code.

Multi-dimension array value sorting in PHP

I am building up an array with a set of database fields with information about table, actual field name and descriptive field name as a multi-dimensional array. Here is what it currently looks like:
$Fields['User']['ID'] = "User ID";
$Fields['User']['FirstName'] = "First Name";
$Fields['Stats']['FavouriteOrder'] = "Favourite Item Ordered";
$Fields['Geographic']['Location'] = "Current Location";
$Fields['Geographic']['LocationCode'] = "Current Location Code";
Okay, this is fine, but I am piping this into a system that allows exporting of selected fields, and in the end I want to foreach() through the different levels, extract the data and then ultimately have all the descriptive fields to be displayed sorted alphabetically using their descriptive name. So ultimately in the order: Current Location, Current Location Code, Favorite Item Ordered, First Name then User ID - obviously keeping index associations.
I can't use usort() and I can't use array_multisort()... or maybe I can and I just don't know how. usort() seems to need a key to sort by, but I have variable keys. array_multisort() just seems to do the same as sort() really.
This is for a 2D array only. Not the most elegant piece of code I've written, but it works...
foreach($Fields as $key=>$var) {
ksort($var);
$Fields[$key]=$var;
}
ksort($Fields);
Let me rather give a real-life data example, as opposed to fake data because the fake data nearly confused me. So, fake data is commented.
/*
$Fields['User']['ID'] = "User ID";
$Fields['User']['FirstName'] = "First Name";
$Fields['Stats']['FavouriteOrder'] = "Favourite Item Ordered";
$Fields['Geographic']['Location'] = "Current Location";
$Fields['Geographic']['LocationCode'] = "Current Location Code";
*/
$Fields['Product']['ReferenceNumber'] = "Product Reference Number";
$Fields['Product']['Halaal'] = "Halaal Status";
$Fields['Product']['Kosher'] = "Kosher Status";
$Fields['Product']['KosherType'] = "Kosher Type";
$Fields['Product']['CuringSalts'] = "Curing Salts Status";
$Fields['Product']['ProductVisibility'] = "Product Visibility";
$Fields['Product']['ProductStatus'] = "Product Status";
$Fields['Product']['PackBarCode'] = "Barcode";
$Fields['Product']['ProductDescription'] = "Product Description";
$Fields['Pack']['PackSize'] = "Pack Size";
$Fields['Pack']['PackSizeNumeric'] = "Numeric Pack Size";
$Fields['Allergens']['ContainsNuts'] = "Product Contains Nuts";
foreach ($Fields as $key => $value) {
ksort($value);
$Fields[$key] = $value;
}
ksort($Fields);
I'm having one of 'those' Fridays... print_r($Fields) reveals that the keys are being sorted and values are associated, but it's still sorting by the keys and not the end value.
It's almost like i need a reverse sorting system which checks all values first, sorts them and then says 'okay where do you belong ... ah you belong to FieldX in Table Y'
I was hoping there was a sneaky clever way to do it, perhaps there is, but I guess I'll write a function to parse through the data, write a reversed array and then re-write the original in value-order. Hectically inefficient, but it'll do.
Still open to better suggestions though!
I literally had to work this out yesterday for a project I was working on - here's my code:
Resource array looks like this:
$resource[$count]['title']
$resource[$count]['filepath']
$resource[$count]['filename']
$resource[$count]['taxonomy'][0]
A couple of sort functions to sort by title ASC or DESC
function compare_asc($a, $b) {
return strcmp($a['title'], $b['title']);
}
function compare_desc($a, $b) {
if ($a['title'] == $b['title']) {
return 0;
}
return ($a['title'] > $b['title']) ? -1 : 1;
//return strcmp($a['title'], $b['title']);
}
And finally use usort to do the dirty before you loop through $resource and output whatever it is you need.
usort($resource, "compare_asc");
Okay, it's not elegant at all. So I don't encourage using this and will look for a better way down the line. But here's the solution I've got that works with the examples above and below.
Unfortunately, the way I want it to be, the array HAS to contain a preceding 'ordering' number so, I suppose it's a fail on my part from the very beginning. But it works now.
$TempDescArray = array();
$TempFieldArray = array();
$TempTableArray = array();
$Pointer = 0;
foreach ($Fields as $Table => $FieldsArray) {
foreach ($FieldsArray as $Field => $Description) {
$TempDescArray[$Pointer] = $Description;
$TempFieldArray[$Pointer] = $Field;
$TempTableArray[$Pointer] = $Table;
$Pointer++;
}
}
asort($TempDescArray);
$Fields = array();
$Pointer2 = 0;
foreach ($TempDescArray as $Pointer => $Description) {
$Fields[$Pointer2][$TempTableArray[$Pointer]][$TempFieldArray[$Pointer]] = $Description;
$Pointer2++;
}

Categories