YII Inserting Multiple Dynamic rows - yii

This is my code in view/_form.php
<tr>
<td>
<?php
$criteria = new CDbCriteria;
$criteria->order = 'ScriptArgumentClassType'; //or whatever field
echo CHtml::activeDropDownList($meta,**'[]ScriptArgumentClass_id'**,CHtml::listData(AutoScriptArgumentClass::model()->findAll`enter code here`($criteria),'id','ScriptArgumentClassType'), array('prompt'=>'Please select Argument class type'));
?>
</td>
<td>
<?php
$criteria = new CDbCriteria;
$criteria->order = 'TnType'; //or whatever field
echo CHtml::activeDropDownList($meta,**'[]Type_id'**,CHtml::listData(AutoTnType::model()->findAll($criteria),'id','TnType'), array('prompt'=>'Please select TN type'));
?>
</td>
<td>
<?php
$criteria = new CDbCriteria;
$criteria->order = 'service'; //or whatever field
echo CHtml::activeDropDownList($meta,'**'[]Service_id'**,CHtml::listData(Service::model()->findAll($criteria),'id','service'), array('prompt'=>'Please select Service'));
?>
</td>
<td>
<?php
$criteria = new CDbCriteria;
$criteria->order = 'Manufacture'; //or whatever field
echo CHtml::activeDropDownList($meta,'**[]Cpe_id'**,CHtml::listData(AutoCPE::model()->findAll($criteria),'id','Manufacture'), array('prompt'=>'Please select CPE'));
?>
</td>
</tr>
this is a row of 4 columns. Actually I have used the jquery code to get rows inserted dynamically on clicking AddNew. Works Fine. Now I am trying to save it to the database using yii.
$metadatas=$_POST['AutoTestScriptMeta'];
foreach ($metadatas as $metadata ) {
$meta=new AutoTestScriptMeta;
$meta->attributes = $metadata;
$meta->save(false);
}
Here I am doing foreach for inserting multiple rows into one table.
It gets inserted in this way for 2 inputed row values
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 2
1 0 0 0
0 3 0 0
0 0 4 0
0 0 0 2
instead of
1 2 3 2
1 3 4 2
instead of inserting 2 rows it inserts 8 rows each with a column value.
See the code in bold (** **). That is how I am declaring it as an array.
Any help is appreciated. Please let me know if you need more details.

The problem is quite straight forward here..
What is happening is your jquery is making a post request for each attribute or in other words with data of single column in database..Thats why you are getting only one filed set in each metadata variable which corresponds to one post request..
So what you can do is either merge 4-4 requests before saving..or make your jquery submit all 4 drop down values...
First one code I can do for you,
$metadatas=$_POST['AutoTestScriptMeta'];
$iterations=count($metadata)/4;
$clubbed_array=array();
for($i=0;$i<$iterations;$i++)
{
array_push($clubbed_array,array($metadata[$i*4+0][0],$metadata[$i*4+1][1],$metadata[$i*4+2][2],$metadata[$i*4+3][3]));
}
foreach ($clubbed_array as $clubbed_value ) {
$meta=new AutoTestScriptMeta;
$meta->attributes = $clubbed_value;
$meta->save();
}

Related

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();
}

AutoIncrement SerialNo not working correctly when using PagedList.Mvc

I am using PagedList.Mvc in my project and its working absolutely fine.But the problem is after installing it serialno is not autoincremented.
For example if i've 20 records in the db and showing only 10 records in the page listed from 1 to 10,on selecting second page instead of showing serialno from 11 to 20,its still showing serialno as 1 to 10 .
The code is given below
<tbody>
#foreach (var item in Model.Select((x, i) => new { Data = x, Index = i }))
{
<tr>
<td>#(item.Index+1)</td> `SerialNo is displayed here`
<td>#Html.DisplayFor(modelItem=>item.Data.SITENAME)</td>
<td>#Html.DisplayFor(modelItem=>item.Data.REMARKS)</td>
</tr>
</tbody>
How can i retain the value of last record and increment onselecting the second page ??
Assuming that your model here is PagedList<T>, then you would need to calculate the current number using:
#(item.Index + ((Model.PageNumber - 1) * Model.PageSize) + 1)

SUM and GROUP BY and Difference of colums

i have a table t1
ag_name ag_start ag_end
a 10 20
c 30 50
a 60 70
c 70 75
i want to have this :
ag_name numberOfCards
a 20
c 25
which means a has (20-10) + (70-60) = 20 Cards.
then please help me with the QUERY ??
select ag_name, sum(ag_end - ag_start)
from the_unknown_table
group by ag_name
Please try:
select
ag_name,
sum(ag_end-ag_start) NoOfCards
From t1
group by ag_name
Select SUM(ag_end - ag_start) as numberofcards, ag_name From table
Group by ag_name
the query works fine when i click on go in DirectAdmin , but when i write the code to show the recordset it shows different result .
my model :
public function getAgents(){
$db =JFactory::getDBO();
$query_Recordset1 = "SELECT *,SUM(ag_end - ag_start) AS ag_num FROM #__basak_agent group by ag_fname";
$db->setQuery($query_Recordset1);
return $db->loadAssoc();
}
and this is my .php code :
</thead> <?php
$count=0;
$result=$this->get('Agents');
foreach( $result as $row_Recordset1 )
{
$count++;
?>
<tr>
<td> <?php echo $count ?></td>
<td> <?php echo $row_Recordset1['ag_fname'] ; ?></td>
<td> <?php echo $row_Recordset1['ag_lname'] ; ?></td>
<td> <?php echo $row_Recordset1['ag_num'] ; ?></td>
</tr>
<?php } ?>
</table>
but in code shows :
1 a a a
2 a a a
3 2 2 2
there is no field like this in table , i dont know where it comes from ??!
anybody have suggestion , where i have to look for it ? what is wrong with my code ?

Auto navigate (scroll) to certain table row

I have a table with few thousand records on few pages in a simple html table.. I made a search function that works fine apart from one thing... It displays only one result in a table (which is great cause it means it works!).But... I was wondering is there a way to display back the table with all records, with the one that i was searching for in the middle and highlighted? Here's a simplified table that I have :
<table class="nogap" cellpadding="1" bgcolor="#00000" cellspacing="1" style="margin:110px 0 0 5px; width:100%; border-color:#B6D6F6;" >
<tbody>
<?php include 'dbconn.php';?>
$con = mysqli_connect($host,$user,$pass,$db) or (header( 'Location: errorpage.php' ));
if (mysqli_connect_errno($con)) { header( 'Location: errorpage.php' ); }
$sql = "SELECT * FROM $tb1 ORDER BY (Serial_num +1) LIMIT $offset, $rowsperpage";
$result = mysqli_query($con, $sql) or (header( 'Location: errorpage.php' ));
$row = mysqli_num_rows($result);
while ($row = $result->fetch_assoc())
{
$product = $row['Prod_type'].$row['Serial_num'];
<tr id="mstrTable" class="lovelyrow">
<td width="5%"><?php echo $product;?></td>
<td width="5%"><?php echo $row['Customer'];?></td>
<td width="7%">
<a href="#"
onmouseover="ajax_showTooltip(window.event,'getptn.php?prd=<?php echo $p;?>',this);return false"
onmouseout="ajax_hideTooltip()">
<?php echo$row['Prod_info'];?>
</a>
</td>
</tr>
}
</table>
Thanks!
First of all don't give the same html id attribute to each row (mstrTable). Html Ids should be unique per page.
Instead mark table rows with unique ids, eg:
$html .= "<td id='row_".$row['id']."'>"
Then do a search query first, remember item id, figure out what page should be queried, query the whole page, attach classess 'greyedout' and 'highlighted' to rows accordingly and then you might try this javascript function to scroll down to the item:
https://developer.mozilla.org/en-US/docs/Web/API/element.scrollIntoView

MySQL code fails to display category name (WordPress database)

Why does this code fail to display the category name "Apples" using the current WordPress taxonomy system? The Category names are stored in the $wpdb->terms table (wp_terms).
<?php
$ra_category_id = 3;
$ra_category = $wpdb->get_results("SELECT name FROM $wpdb->terms WHERE term_id = '3'");
$ra_category_name = $ra_category->name;
?>
<h3>Category: <?php echo $ra_category_name; ?></h3>
The table rows are
term_id name slug term_group
1 Uncategorized uncategorized 0
2 Blogroll blogroll 0
3 Apples apples 0
4 Bananas bananas 0
$ra_category is the following array:
array(1) {
[0]=>
object(stdClass)(1) {
["name"]=>
string(8) "Apples"
}
}
So what you want is:
$ra_category_name = $ra_category[0]->name;
When dealing with query results, always check the whole result with a var_dump(), it helps.
(note that you're also using $ra_category_id but then hardcoding the value "3" in your query)