how to get result from array? - php

I have an array as below :
Array
(
[VA-ecoclsqty2_0_R79_31_9_1Room1]=>2",
[VA-preco2-0_R79_31_9_1Room1]=>50.00",
[VA-amt_preco2-0_R79_31_9_1Room1]=>25.00",
[VA-busclsqty4_0_R79_31_9_1Room1]=>2",
[VA-prbus4-0_R79_31_9_1Room1]=>36.00",
[VA-amt_prbus4-0_R79_31_9_1Room1]=>18.00",
[VA-busclsqty5_1_R79_31_9_1Room2]=>1",
[VA-prbus5-1_R79_31_9_1Room2]=>17.00",
[VA-amt_prbus5-1_R79_31_9_1Room2]=>17.00",
[VA-ecoclsqty6_1_R79_31_9_1Room2]=>3",
[VA-preco6-1_R79_31_9_1Room2]=>28.00",
[VA-amt_preco6-1_R79_31_9_1Room2]=>14.00",
)
I want to insert above data as below rule...
insert data 8 (values as 2+2+1+3) rows where
2 row insert on VA-ecoclsqty.... - amount = 25.00, roomcode = R79_31_9_1, unique_id = 1 for Room1
2 row insert on VA-busclsqty.... - amount = 18.00, roomcode = R79_31_9_1, unique_id = 2 for Room1
1 row insert on VA-busclsqty.... - amount = 17.00, roomcode = R79_31_9_1, unique_id = 3 for Room2
3 row insert on VA-ecoclsqty.... - amount = 14.00, roomcode = R79_31_9_1, unique_id = 4 for Room2
and more for different rooms for multiple rows.
How can i insert it?
And please remember that every key is different from each other.
Please reply soon and try to help.

To use ajax for the form I'd look at jQuery ajax function. To process the form and perform a search in you DB you'll have to setup a URL for the form to post to that does the processing and returns the correct response.

Related

PHP increment statement according to value in database

I have created a PHP form with values that are inserted in the database. Fields dropdown boxes that are passed to the database after submit. According to what the user chooses in the dropdown box, score increases.
It contains age (dropdown box, >20 <20), degree (high school, university), etc.
What I want to build,is a function to add score values according to the choice of the dropdown box. Make specific column values a variable which adds scores points.
Every time someone inserts a specific choice, it should get a different amount of points.
Pseudocode, for score:
i = 0.
If dropdown.age>20 (choice 1), 5 points, i= i +5.
If dropdown.age<20, 10 points, i= i +10
If dropdownchoicebox.education = high school, 5 points, if university, 10 points.
Same for database
If age-row.value>20, i +5 else i+10
if education-row.value = highschool, i= i+5 else i + 10
And it should continue giving "score points", according to each specific choice in every field.
The code so far shows the database values. Each insert is in a different row until it runs out rows. For instance, age would be row 4, education row 5, etc.
<?php
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// Select all records from the user profile table.
// Order it by the ordering field.
$query->select($db->quoteName(array(age, education, FieldName, FieldValue)));
$query->from($db->quoteName('table'));
// Reset the query using our newly populated query object.
$db->setQuery($query);
$row = $db->loadRow();
print_r($row);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
?>
Result: array value
Array ( [0] => over 20 [1] => highschool [2] => result 2[3] => result3
Names of rows: [0] = age, [1] = education.
What I want to do:
Button onclick: calculate points
function calculate points
//i for score
i=0
As 0 = over 20, i=0+5 = 5
as 1 = highschool i= 5+5 = 10

checkbox inputs and DB search

I'm tagging Movies and store this into a Database.
a tag_id could be a car, train, boat and details could be color or specific types.
DB
movie_id | tag_id | tag_detailsid
2612 | 75 | 1
2612 | 10 | 3
2612 | 12 | 2
The tags are submitted via a form with checkboxes (all checkboxes checked are added to the db)
Now..
How do I keep track with checkboxes I uncheck..at a later stage
So looking at the above example.. for movie_id 2612, I uncheck tag 12 with id 2 as details.
So $_POST holds only 75-1 and 10-3....12-2 should be deleted from dB.
So I thought.. I simply go through the dB with a while loop and compare the db value with the values I get from the checkboxes (via the $_Post method)..
I count the values in the $_Post and it shows 2 (checkboxes checked).
Database however holds 3 entries ($numberofrecords) for that specific Movie.
My script so far...
$sql_query = "Select tag_id, tag_details_id FROM movie_tags WHERE movie_id = '2612";
$report = MYSQL_QUERY($sql_query);
$numberofrecords = mysql_num_rows ($report);
while ($report_row = mysql_fetch_array($report))
{
$thistag_id = $report_row['tag_id'];
$tag_details_id = $report_row['tag_details_id'];
foreach($_POST as $checkbox_id => $value)
{
if ($thistag_id == $checkbox_id) // check if DB tag equals checkbox value
{
echo "checkbox value is checked equals value in DB";
}
}
}
How do I track the record I need to delete from the database?
Sometimes the easiest solution is the way to go.
Unless you have a compelling reason NOT to, you can simply delete all of the tag records before saving, then insert only those that were checked:
sample code using OP's selected db driver
$sql = 'DELETE FROM movie_tags WHERE movie_id=2612';
mysql_query($sql);
foreach($_POST as $checkbox_id => $value) {
// save your records here
// To show you the code I'd need to see the HTML for the checkboxes.
}
NOTE: You should not be using mysql_. Use PDO / mysqli instead: Why shouldn't I use mysql_* functions in PHP?
A simple solution would be to delete every rows based on your primary key such as movie_id in your case and do insert in loop for every checked checkboxes. That is:
Delete rows based on movie_id
Loop through POST data and do insert

php pdo code to create rows dynamically from user input?

column name = receipts (txtbox)
if user enter receipts = 5 then insert 5 rows in database.
data insert in table like this..
receipts
1
2
3
4
5
if user enter again receipts = 3 then again insert 3 rows in database.
6
7
8
like wise....
plz suggest me how to do this....
below is my code to save in php pdo...
i tried below code this is working but not inserting no in sequential order....
if(isset($_POST['save']))
{
$book_no = $_POST['book_no'];
$receipt= $_POST['receipt'];
for($row=1;$row<=$receipts ;$row++)
{
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
}
}
First you have to change the following code:
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$receipt));
like this (the receipt number is the $row var, not the $receipt var)
$insertrow = $database->insertRow("INSERT INTO scheme_master (book_no,receipt,created) VALUES (:book_no,:receipt)",
array(':book_no'=>$book_no,':receipt'=>$row));
If you want that, the next time, it start from the last number inserted (that is, for example, start from 6, if the previous time you have inserted 5 receipts), you have to query the DB to get the the current max value, and then start from it. To get the max, a query like this should work:
SELECT MAX(receipt) FROM scheme_master where book_no = :book_no

multiply data from database to value from textbox

i have 1 table named tbl_sales. the ff data in tbl_sales are
id | total |
1 | 100 |
my question is this. i want to update "total" by multiplying it to whatever values in textbox.
this is my code so far to display textbox. i echo it so that everytime i add order it will appear on every table row
echo '<td>'.$vats_tot.'</td>'; //- the value display in this row is from database.
echo 'input type = "text" name = "ds"/>;
my problem is this. i want to multiply this the value from this textbox to "$vats_tot" which is from database value. can it be possible to multiply this?
Try like
$txt_val = $_POST['ds'];
$sql = "UPDATE tbl_sales SET total = total * $txt_val";
sql query for your problem
$textbox_value=$_post['textbox_id'];//To get TextBox Value
update **[table_name]** set total=(total*$textbox_value) where id=1;//To update the data into the table

Display random records in each page but not the same

Well guys i have this query
$mysql = "select * from xxx where active = 1 order by Rand() limit $start,12";
mysql_query($mysql);
Everything works great so far.
I want: when i am pressing the next button (page 2 or three etc) to see the next 12 random records but do not display the first 12 random records that i had in my previus page!
Thank you all!
p.s Sorry guys for my bad english!
Just try to retrieve the data you need in an array, randomize it with shuffle() in PHP, and paginate the result with some JQuery, it will be awesome, just one query and no refresh. ;)
You need to keep one array (e.g $arrRecordIds) to track all the id's of records shown on previous pages.
When you are on first page:
$arrRecordIds=array(); // Empty array
When you are on second page:
$arrRecordIds=array_merge($arrRecordIds, $arrNewRecordIds);array_unique( $arrRecordIds );
If your select query simply concat- where id NOT IN ( implode(',', $arrRecordIds ) )
Here $arrNewRecordIds should contains id's of the records on the page.
You can keep track of the previously shown records' ids and put them in an array.
In your query use id NOT IN (array)
Apply the concept of Systematic Random Sampling,
Number the records N, decide on the n (pagination size, eg: 10, 20)
(sample size) that you want or need k = N/n = the interval size
Randomly select an integer between 1 to k then take every k th unit
Refer: http://www.socialresearchmethods.net/kb/sampprob.php
Try using the following script in your showdata.php file
$per_page = 12;
$sqlc = "show columns from coupons";
$rsdc = mysql_query($sqlc);
$cols = mysql_num_rows($rsdc);
$page = $_REQUEST['page'];
$start = ($page-1)*12;
$N = 1000; //Total rows in your table (query to get it dynamically)
$n = $per_page;
$k = ceil($N/$n);
$range[] = $page;
for($i=1;$i<$n;$i++) {
$range[] = ($page+$k)*$i;
}
$sqln = "SELECT * FROM ( SELECT #rownum:= #rownum+1 AS rindex, n.* FROM xxx n, (SELECT #rownum := 0) r ) AS rows WHERE rindex IN (".implode(',',$range).")";
$rsd = mysql_query($sqln);
SOLUTION - that works a treat.
do a select random search of all required records
generate a random user-id eg. "smith".rand(1000,10000)
form a string of all random keys upto required no of records per page
insert above in a table/field containing a corresponding page no.
repeat/loop above until no more pages/recs remaining - use array_splice(str,from,to) - then use $notscreen = print_r($splice, true) for string storage to table -> randompages:
tb.rec-no | user-id | pageno | string ( with keys of recs upto recs/page)
122 | aj7894 | p1 | [0]=>100[1]=>400[2]=>056[3]=>129
123 | aj7894 | p2 | [x]99=>[x]240=>[x]7895[x]458=>320
... upto whole array of pages /no of records / all pages - no duplication of data - only 1-column of key of recs stored in random as retrieved
use user-id & pageno with WHERE to pull out random keys for that individual user & page
convert string back to array and pull out matching key recs for specific pages using the array in a SELECT WHERE query with implode
re-circ [ user-id & pageno ] using $_GET/POST for duration of search/view - reinitialise when new view or new search commences
notes:
-better to use list for search - but requires more work to format string - should give random page results as originally stored
problem with array matching is it orders records per page; lowest being first - not so random for the page display
temp table no good - because cannot be accessed when script is thrown back to server for 2nd and more time - it's lost from memory by mysql
php rules - no flimsy cookies or java-script !
BIG PROBLEM - SOLVED.
re-compsense for help received from your posts / answers.
Happy Days!

Categories