Webform : how to count and live display results - sql

Update (that one works)
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load($nid);
if ($node->type == 'webform') {
$count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d', $nid));
$atelier_1 = "sources" ;
$sql = "SELECT count(*) FROM {webform_submitted_data} WHERE data LIKE \"".$atelier_1."\" ;";
$count_atel_1 = db_result(db_query($sql));
}
}
echo $sql;
echo $count_atel_1;
?>
This webform has been submitted <?php print $count ?> times.
We'd like to use a webform so that our students should register on some workshops.
The webform works great. Now we'd like to display live the number of students that are already register in on of the workshops so that the other should know if there remains some possibilities of registration (each workshop can only accept 20 students at the same time)
I'm trying with that that doesn't work ($atelier_1 = "sources" ;) is the name of one workshop :
<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load($nid);
if ($node->type == 'webform') {
$count = db_result(db_query('SELECT count(*) FROM {webform_submissions} WHERE nid = %d', $nid));
$atelier_1 = "sources" ;
$count_atel_1 = db_result(db_query('SELECT count(*) FROM {webform_submitted_data} WHERE data LIKE %d', $atelier_1');
}
}
echo $count_atel_1;
?>
Any help or suggestion welcome

The only way you can show that live, is by creating an ajax in drupal, or with drupal behaviours, or with javascript.

Related

duplication of products in prestashop backend and front

I have imported 16 000 products, and now I have 57 000 products.
Can any one help me please to delete all duplicated products as you see in picture.
Product with different id and same reference.
Image backoffice:
Image front:
You can try something like that, you can test it in development environment, you should create a php file in your root project.
require_once('config/config.inc.php');
require_once('init.php');
$query = "select id_product,reference from " . _DB_PREFIX_ . "product where active=1";
$res = Db::getInstance()->ExecuteS($query);
foreach($res as $prod){
$query = "select id_product from " . _DB_PREFIX_ . "product where reference=$prod['reference']";
$res = Db::getInstance()->ExecuteS($query);
$count = count($res);
if($count){
foreach ($res as $key => $p) {
if (--$count <= 0) {
// to not delete the last occurrence for a given reference
break;
}
$id_product = $p['id_product'];
$product = new Product((int)$id_product);
if($product->delete())
echo 'product '.$id_product.' is deleted';
}
}
}

How do i use JOIN with a get_where clause in CodeIgniter

I'm trying to get the data from two tables after an "Open" button is clicked from previous table UI by sending an Id.
e.g.
<a name="Open" href="<?php echo base_url('welcome/WellProfile/'.$post->Id) ?>">
(https://imgur.com/6IU1Tfo)
So I have two tables - namely "WellDataA" and "WellDataB" , I want to get the data where WellDataB data matches with the WellDataA by WellName(column).
Example:
WellDataA:
Id Platform WellName
.
.
4 ZE ZE-A
5 ZE ZE-B
6 ZE Ze-B
.
.
WellDataB:
Id WellName CompleteDate
1 ZE-A 12/3
2 ZE-B 14/5
3 ZE-C 20/6
This is how my query so far, but it ended up error
public function get_news_by_id($Id = 0)
{
if ($Id === 0)
{
$query = $this->db->get('WellDataA');
return $query->result_array();
}
$this->db->select('*');
$this->db->from('WellDataA');
$this->db->join('WellDataB','WellDataA.WellName =
WellDataB.WellName','INNER');
$query = $this->db->get_where('WellDataA', array('Id' => $Id));
return $query->row_array();
}
I expect the output would show ZE, ZE-A, 12/3 when "Open" button is clicked on ZE-A. But the actual output is ZE, ZE-A only. Thank you so much in advance!:)
Try this, your issue is in get_where() so, i removed it and rewrite it
public function get_news_by_id($Id = 0)
{
if ($Id === 0)
{
$query = $this->db->get('WellDataA');
return $query->result_array();
}
$this->db->select('a.Id,a.Platform,a.WellName,b.Id,b.CompleteDate');
$this->db->from('WellDataA a');
$this->db->join('WellDataB b','a.WellName = b.WellName','INNER'); //changes
$this->db->where('b.Id',$Id); //changes
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row_array();
}else{
return array();
}
}
Firstful I am sorry if my solution won't be a good one,
but I suggest you change the structure of your tables to this:
WellDataA:
Id Platform WellDataB
.
.
4 ZE 1
5 ZE 2
6 ZE 3
.
.
WellDataB:
Id WellName CompleteDate
1 ZE-A 12/3
2 ZE-B 14/5
3 ZE-C 20/6
Since I assume that the used model is relational, one thing must first achieved is to make the tables all relational. Instead of connecting WellDataA and WellDataB with the Id from WellDataB inside WellDataA.
I think you just need to remove some code. Just try this code
public function get_news_by_id($Id = 0)
{
if ($Id === 0)
{
$query = $this->db->get('WellDataA');
return $query->result_array();
}
$this->db->join('WellDataB','WellDataA.WellName =
WellDataB.WellName','INNER');
$query = $this->db->get_where('WellDataA', array('Id' => $Id));
return $query->row_array();
}

Pagination with PDO prepare statement

I am using David Carr's pagination class successfully for a whole-table call (i.e. where I'm calling all the rows in the table without filtering). After including my config and the class, I accomplish it this way:
$pages = new Paginator('3','p');
$stmt = $dbh->query('SELECT count(id) FROM sermons');
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->query('SELECT * FROM sermons ORDER BY date_preached DESC '.$pages->get_limit()); ?>
<div class="paging"><p><?php echo $total; ?> sermons found</p> <?php echo $pages->page_links();?></div>
<?php
foreach($results as $row)
Etc.
That works great, but now I'm trying to implement it for the result of user filtering via forms, and because I can't use an unnamed placeholder in the query, I'm trying to do it with a prepare (which I'm guessing is more secure anyway):
$pages = new Paginator('2','p');
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$stmt = $dbh->prepare("SELECT count(id) FROM sermons WHERE series = ?");
$stmt->execute(array($series));
$row = $stmt->fetch(PDO::FETCH_NUM);
$total = $row[0];
//pass number of records to
$pages->set_total($total);
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ? ".$pages->get_limit());
$results->bindParam(1, $series, PDO::PARAM_STR);
$results->execute(array($series));
// the call
echo $total; ?> sermons found</p> <?php echo $pages->page_links();
When I first get to the results page from the form, it looks like everything is working properly. The call correctly identifies the number of sermons found for the search result, and displays the first two (or however many I set in the $pages statement). But when I click to another page result, no sermons display, the count is gone, and my !isset(found_rows) echo returns a zero as well.
I was originally thinking the issue was that at first I was trying to do this without binding. That is questionable, since without the pagination I can display the page without binding. In any case, I've been trying that with both bindValue and bindParam, but with no luck.
Am I mishandling something in the prepare statement? Or what?
Note: I have the results page displaying fine without the pagination with this:
$series = isset($_POST['series']) ? $_POST['series'] : false;
try {
$results = $dbh->prepare("SELECT * FROM sermons WHERE series = ?");
$results->execute(array($series));
// Etc.

Yii Clistview only print once

I want to echo a div only once in a Clistview, the items are order by status, so, i want to print status 1 -> all the items, and then status 2 -> all the items with that status, I tried viewData, but I dont know how to change the value of the flag.
INDEX VIEW:
<div class="modal-body">
<?php
$activos_flag = 1;
$inactivos_flag = 1;
?>
<?php
$this->widget('zii.widgets.grid.CListView', array(
'id'=>'incs',
'summaryText'=>'',
'dataProvider'=>$dataProviderInc,
'itemView'=>'_incidencias',
'viewData'=> array('activo'=> $activos_flag,'inactivo'=>$inactivos_flag),
));
?>
</div>
_INCIDENCIAS VIEW:
<?php
if ($data->activo == 1 and $data->incidencia_estado == 1){
echo ('<label class="incidencias">ACTIVOS</label>');
$data->activo = 0;
}
if ($data->inactivo == 1 and $data->incidencia_estado == 0){
echo ('<label class="incidencias">INACTIVOS</label>');
$data->inactivo = 0;
}
?>
You need to get the value not from the array $data ($data->inactivo) but directly from a variable $inactivo. But in any case, at each iteration, the value of these variables will again be equal to 1. In this case, you can use the following approach:
Before widget declaration:
Yii::app()->params['activos_flag']=1;
Yii::app()->params['inactivos_flag']=1;
and in parial view:
if ( Yii::app()->params['activos_flag'] == 1 ){
echo ('<label class="incidencias">ACTIVOS</label>');
Yii::app()->params['activos_flag'] = 0;
}
if ( Yii::app()->params['inactivos_flag'] == 1 ){
echo ('<label class="incidencias">INACTIVOS</label>');
Yii::app()->params['inactivos_flag'] = 0;
}

PDO row doesn't exist?

Below is my code:
<?php
$url = $_GET['url'];
$wordlist = array("Www.", "Http://", "Http://www.");
foreach ($wordlist as &$word) {
$word = '/\b' . preg_quote($word, '/') . '\b/';
}
$url = preg_replace($wordlist, '', $url);
?>
<?php
$oDB = new PDO('mysql:dbname=mcnsaoia_onsafe;host=localhost;charset=utf8', 'mcnsaoia_xx', 'PASSWORD');
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt=ja");
$hStmt->execute(array('hjemmside' => $url));
if( $row = $hStmt->fetch() ){
echo "EXIST";
}else{
echo "NOT EXIST";
}
?>
My problem is that it says NOT EXIST, because I know that there is a row which should be found with the following query:
SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt=ja
So why does it say not exist? I have absolutely no idea :(
Instead of
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside
AND godkendt=ja");
try
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside
AND godkendt='ja'");
The left is most likely a column and the right side is a string? I don't speak your language, but this is the first thing coming to my mind.
You should surround with quotes your not integer variable in your query
AND godkendt='ja'
Or maybe let pdo deal with it
$hStmt=$oDB->prepare("SELECT * FROM users WHERE hjemmside = :hjemmside AND godkendt = :ja");
$hStmt->execute(array(':hjemmside' => $url, ':ja' => 'ja'));
//^ i added : for placeholder here, you missed it
I would also rather check if rows are returned
if($hStmt->$eowVount() > 0){
$row = $hStmt->fetch()
echo "EXIST";
}else{
echo "NOT EXIST";
}