I have to make an array with the POSTED value of one SELECT. The SELECT selects the products ONE BY ONE. First I choose one product and POST it then another product and I POST the SECOND ONE and so on.....
I want to create an array of the ID of the products that are posted by the SELECT but this array has to grow while I introduce more and more products.
I have use this but It makes the array with only the last product I have choosen.
foreach($_POST['idproducto'] as $key => $val) {
$cadenaides = $cadenaides . "$val,";
}
$cadenaides = $cadenaides . 1;
I would like the array to have all the ID of the products I choose ONE BY ONE in the SELECT.
Seems to me like you want to assign a number to each posted value. You can do this like so:
foreach(...) {
$cadenaides[] = $val;
}
Your values will the be stored in an array. You can check your array with print_r($cadenaides);.
Reading the comments above and assuming that you use MySQL I would suggest the following:
SELECT GROUP_CONCAT(id_producto SEPARATOR ',') FROM producto WHERE .... put your conditions here ..;
This will concatinate all IDs in a single string like that 1,2,3,5,8,9... in a single result, after that you can do just one POST request. Very usefull in many cases BTW.
The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. Could be very large - max: 4294967295 for 32-bit system.
Related
the script querys database and retrieves a single entry that has mulitple numbers
SELECT jnum from database where x = y
output = 11111,22222,33333,44444
So i explode that on , and get $variable[0] = 11111 and $variable[1]= 22222
What i want to do is perform a query on another table using each of those numbers (numbers will be different each time and there may be any number of numbers).
is there a way to structure a foreach for each entry in the array or a while loop that counts so that i can query the database for each of the values i get from output above.
i don't know if i am conveying what 'im trying to do here very clearly so i apologize in advance.
i get a single entry for the database table and it contains a string (11111,22222,33333)
i explode on , and get the array variable[]
there will not always be 3 entries sometime there could be 5 or 7 or 10 or 1 but each one will be unique.
but for each value i want to query a db table and retrieve all the rows that have that single number($variable[]) as an entry.
Not sure if a loop count or a foreach statement would work. any ideas?
Well assuming these are values in a single column there is no need to look you can use WHERE ... IN:
SELECT * FROM the_other_table WHERE some_col IN ('11111','22222','33333')
Check out foreach loops - http://php.net/manual/en/control-structures.foreach.php
foreach ($variable as $value) {
$myquery = "some query using $value";
// then execute your query
}
I have to retrieve the history of a user and I have 4 tables whose data depend on each other.I can retrieve the data using loops,but I instead used the "where IN ()" clause and I implode the output of the previous query.However,if the list I provide to "where IN()" is empty it return an error.Is it that IN() cannot be empty?
When imploding an array for the IN clause, i do one of two things
1: Check if you even need to run the query at all
if(!empty($some_array)) {
//run mysql query
}
else {
// if you need to do something if the array is empty, such as error or set some defaults, do it here
}
2: A value in the array initiliser which is not ever in the database (for example, if im selecting based on a auto incrememnt id, i use zero as a default array value to stop any issues with empty data sets, as zero will never be in my id column).
$some_array = array(0);
You can add an empty value to the start, such as IN (0,your values here)
Say I have the following entity called inventory:
I would like to know if there are database, sql or magento actions or any other methods to produce:
I have solved this by iterating through the entire collection and inserting into a temporary table : i.e
$inventorySet = Mage::getModel('custom/module')->getCollection(*);
foreach($inventorySet as $item)
{
$this->insertItem($item->getSku());
}
}
public function insertItem($sku)
{
//insert sku if it does not exist in the temp set
// if it does exists add one to the QTY field
}
I can then retrieve what I want. I don't see an issue with it, but my entity is a lot larger than the example as well as the set of data containing anything between 2000 - 15 000 rows. is there not a more efficient way of doing this?
EDIT: In words, I would like to search the collection for occurrences of the "sku" field and return an array of unique sku's and the number of times it was found in the initial collection.
To get a particular set of column values from a collection you can use the getColumnValues() method.
For example, using a Magento product collection, this would return an array containing the sku attribute value for every product:
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('sku');
$skus = $collection->getColumnValues('sku');
Finally, to answer the second part of your question, to count the occurence of each unique value:
array_count_values($skus);
This will give you a nice associative array of sku => occurrence count.
I want to code a script that will echo an amount of images based on the mysql_num_rows(); function.
Like, I'm storing 3 in a row. Then I'll create:
$images = mysql_num_rows($count);
Then I would like it to echo 3 images.
Is this possible?
If you don't get the question or want me to rewrite it, please just tell me.
Then i think, all you need to do is fetch the rating (i.e. a number) from the database and just use that number directly to display the number of stars. Coz mysql_rows_count would give you the total number of records in a result set which is returned as a result of a select query on multiple columns, but in your case you only need to access one column which would contain the rating and another column which would probably contain user id or some sort of key to associate the ratings with. The result set can be fetched as an associative array which can then be used by indices to fetch the right column. Have a look at this: PHP-mysql_fetch_array() .
EDIT: just to sum up (not the actual syntax) :
$array = mysql_fetch_array(<your select query fetching only the needed columns>);
$images = $array[0] // assuming the rating number is at the 0th index
//image display code, do your stuff
So here is what I did:
I had the numbers from 1-5 in my database in a row called 'rating'. Now when I picked this out of the database I stuffed the number (1-5) into a variable called '$starRating'.
for($i=1;$i<=$starRating;$i++){
echo "<span class='star'><span class='starText'>$i</span></span>";
}
Then I created a class called star and starText:
.star{
width:18px;
height:18px;
background:url('../images/design/smallImg/star.png') no-repeat;
text-align:center;
float:right;
margin-top:2px;
}
/* star content */
.starText{
display:none;
}
.starText is made to hide the text that would've been from the for() loop.
* I don't know if you can avoid the text another way around *
This works for me perfectly.
I wrote this to help people searching for an easy solution to this.
I have a MySQL field which stores an array with 3 values:
array(item1=>1123, item2=>5454, item3=>23432)
How can I query the database with PHP so that I get only distinct values of item2 and then arrange those results by the values of item3?
A lot more information is needed - like, how your database is structured.
Look into the DISTINCT() function and the ORDER BY clause of SQL
It much easier to store your array into text and something you can separate later. Using php you can do this. I'll try to work with your data here into something you can use. says you have the field items. If instead you had an array such as.
$items = array(1123,5454,23432);
You can then implode it with a symbol such as:
$items = implode('|',$items);
and then store this in the database under a fields such as items that would look like this:
1123|5454|23432
Then when you want to grab the data you just need to explode it with the same symbol:
$items = explode('|',$row['items']);
and you are back with your dataset:
$items[0] = 1123
$items[1] = 5454
$items[2] = 23432
and then can access the second item by grabbing element one of the array:
$items[1]
Hopefully that gives you a better understanding of it.