Update query for loop is not working - codeigniter

I have a query like this inside for loop in codeigniter. But it executes with another values. Not with the values getting through POST method
$j = $_POST['hidden'];
$inv_id = $_POST['invoice_id'];
$sum = '';
for($i = 1; $i <= $j; $i++){
$wh_quantity1 = $_POST['quantity'.$i];
//print_r($wh_quantity1);
if($wh_quantity1 ==''){
$wh_quantity = 0;
}
else{
$wh_quantity = $wh_quantity1;
}
$query = "UPDATE tb_warehouse_stocks SET wh_product_qty = wh_product_qty - $wh_quantity WHERE invoice_id = '$inv_id'";
$this->db->query($query);
$sum += $wh_quantity;
}
Why it is like that. It always updates with greater values than POST value

Put this in .htaccess file
RewriteEngine On
RewriteRule ^ http://example.com/international/university-english-access-course$ http://example.com/website/page/english-access [R=301,L]

Try this in case your dont have all post index
$j = $this->input->post('hidden');
$inv_id = $this->input->post('invoice_id');
$sum = 0;
for ($i = 1; $i <= $j; $i++) {
$wh_quantity = (int) $this->input->post('quantity' . $i);
$sum += $wh_quantity;
}
$query = "UPDATE tb_warehouse_stocks SET wh_product_qty = wh_product_qty - $sum WHERE invoice_id = '$inv_id'";
$this->db->query($query);

Related

How to delete all rows which have value 1 in "status_pesanan" column

I want to delete all rows which have value of 1 in column "status_pesanan" before running create data Laravel
my controller
public function create()
{
$penjualan = DB::table('penjualan')
->groupBy('status_pesanan')
->havingRaw('COUNT(status_pesanan) = 1')
->delete();
$penjualan = new Penjualan();
$penjualan->nama_pemesan = 1;
$penjualan['no_nota'] = tambah_nol_didepan($penjualan->no_nota+1, 5);
$penjualan->alamat_pemesan = 1;
$penjualan->telepon_pemesan = 1;
$penjualan->acc_desain = 1;
$penjualan->total_item = 0;
$penjualan->total_harga = 0;
$penjualan->diskon = 0;
$penjualan->bayar = 0;
$penjualan->diterima = 0;
$penjualan->id_user = auth()->id();
$penjualan->save();
session(['id_penjualan' => $penjualan->id_penjualan]);
return redirect()->route('transaksi-baru.index');
}
If I run the code as above, the output I receive is that all old rows in the database are deleted
You are grouping by status_penasan having its count equal to 1 and call delete upon it. You wanted to remove the records whose status_penasan value is 1. This could be a fix:
$penjualan = DB::table('penjualan')
->where('status_pesanan', 1)
->delete();

explode comma from an array value in codeigniter

I want to explode coma from an array value.
My code is.
$to_ids_string = "";
$to_id = $this->input->post('to');
for ($r = 0; $r < count($this->input->post('to')); $r++) {
if ($to_ids_string != "") {
$to_ids_string = $to_ids_string . "," . $to_id[$r];
} else {
$to_ids_string = $to_id[$r];
}
}
echo $to_ids_string;
$a = explode(',', $to_ids_string);
foreach ($a as $item) {
echo("<li>$item</li>");
exit;
}
when i echo $to_ids_string it will return 2,3 but when i loop in foreach it only return 2 not show 3.
Because of your exit, if you use exit like that, then it is the end of your program and it doesn't echo anymore.
You forget to remove exit; from foreach loop. When you write exit, execution of your code stops. Hence you are not getting desired output.
Happens due to exit.
Please remove exit from your code.

Laravel Blade - loop for

How can i do it:
#for ($i = 1; $i < 11; $i++)
#if ($categorie->t$i != null)
{{$categorie->t$i}}
#endif
#endfor
categorie->t$i = categorie->t1 , t2, t3 ... t10.
Thanks !
You can access the item t$i by concatinating the string 't' with the value of $i, and afterwards accessing this.
First:
$item_name = 't' . $i;
Then:
$category->$item_name
This should do the trick.

Firefox extension to get google pagerank and alexa ranking

I am creating a Mozilla Firefox toolbar to show PageRank and Alexa ranking of current website. One way I came to know is to use XMLHttpRequest in my JavaScript file to get information from a PHP page hosted on my website's server.
The PHP class has this function:
function check($page) {
// Open a socket to the toolbarqueries address, used by Google Toolbar
$socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
// If a connection can be established
if($socket) {
// Prep socket headers
$out = "GET /tbr?client=navclient-auto&ch=".$this->checkHash($this->createHash($page)).
"&features=Rank&q=info:".$page."&num=100&filter=0 HTTP/1.1\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
$out .= "Connection: Close\r\n\r\n";
// Write settings to the socket
fwrite($socket, $out);
// When a response is received...
$result = "";
while(!feof($socket)) {
$data = fgets($socket, 128);
$pos = strpos($data, "Rank_");
if($pos !== false){
$pagerank = substr($data, $pos + 9);
$result += $pagerank;
}
}
// Close the connection
fclose($socket);
// Return the rank!
return $result;
}
Is there a better way to get page ranks in my custom Firefox toolbar without having to host a PHP service?
// Create a url hash
function createHash($string) {
$check1 = $this->stringToNumber($string, 0x1505, 0x21);
$check2 = $this->stringToNumber($string, 0, 0x1003F);
$factor = 4;
$halfFactor = $factor/2;
$check1 >>= $halfFactor;
$check1 = (($check1 >> $factor) & 0x3FFFFC0 ) | ($check1 & 0x3F);
$check1 = (($check1 >> $factor) & 0x3FFC00 ) | ($check1 & 0x3FF);
$check1 = (($check1 >> $factor) & 0x3C000 ) | ($check1 & 0x3FFF);
$calc1 = (((($check1 & 0x3C0) << $factor) | ($check1 & 0x3C)) << $halfFactor ) | ($check2 & 0xF0F );
$calc2 = (((($check1 & 0xFFFFC000) << $factor) | ($check1 & 0x3C00)) << 0xA) | ($check2 & 0xF0F0000 );
return ($calc1 | $calc2);
}
// Create checksum for hash
function checkHash($hashNumber)
{
$check = 0;
$flag = 0;
$hashString = sprintf('%u', $hashNumber) ;
$length = strlen($hashString);
for ($i = $length - 1; $i >= 0; $i --) {
$r = $hashString{$i};
if(1 === ($flag % 2)) {
$r += $r;
$r = (int)($r / 10) + ($r % 10);
}
$check += $r;
$flag ++;
}
$check %= 10;
if(0 !== $check) {
$check = 10 - $check;
if(1 === ($flag % 2) ) {
if(1 === ($check % 2)) {
$check += 9;
}
$check >>= 1;
}
}
return '7'.$check.$hashString;
}
If your PHP code only makes an HTTP request then you can just do the same request from the Firefox extension as well:
var request = new XMLHttpHeader();
request.open("http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=...");
request.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)");
request.send();
However, you should clarify whether this use of a Google server (particularly masquerading as Google Toolbar) complies with their Terms of Service. Otherwise you might find yourself confronted with legal action or at the very least sudden changes in the way this web service works.
As to the hash function: obviously you can either translate this algorithm to JavaScript (which is pretty straightforward from the look of it) or search around to see whether anybody did it already. E.g. I found this JS-based hash algorithm implementation (it's a different algorithm that is prefixed with 8 instead of 7 however, note also that this prefix isn't returned by the hash function but is rather part of the URL there).

EAV_Databasesystem in Magento 1.6

I have a strange problem with Magentos EAV-Databasesystem.I want to store a dataset like the next codeblock. But one value 'StoreId' was not stored. Do you have any Idea why one of eight values can stored in a table.
public function populateEntriesAction()
{
//return;
for ( $i = 0; $i < 5; $i++ )
{
$branchEntry = Mage::getModel('branch/eavisbranch');
$branchEntry->setName('abc ' . $i);
$branchEntry->setStreet('abcdefg ' . $i);
.....
$branchEntry->setStorePickup(false);
$branchEntry->setStoreId('3');
$branchEntry->save();
}
echo 'Done';
}

Resources