I am trying to populate my dropdown list on a form with data from a database, i have tried the following but to no success it gives me an error saying unexpected ; i have tried removing but still getting errors?
echo form::label('myproduct', 'My Product:');
echo form::select('form[myproduct]', $sql = mysql_query("SELECT description FROM claim_incentive"); while ($row = mysql_fetch_array($sql))
{echo "<option value=\"1\">" . $row['description'] . "</option>"; }
I think i could be putting the code in the wrong place as i have two php files, one is the controller, my code below:
public function action_claimincentive() {
$this->template->content = View::factory('crm/uk/claim_incentive_form');
$this->template->content->thanks = false;
$this->template->content->val = '';
$this->template->content->post = '';
if ($this->request->post('form')) {
$post = $this->request->post('form');
$stmt = DB::query(Database::INSERT, 'INSERT INTO `claim_incentive_form_data` (`User Reference`, `Claimant Postcode`, `Purchase Order No.`, `Claimant Email Address`, `Storename`, `Storetown`, `Date of Sale`, `Date of Delivery`, `Acknowledgement No.`, `Product`)
VALUES (:userreference, :claimantpostcode, :orderno, :email, :storename, :storetown, :dateofsale, :dateofdelivery, :acknowledgementno, :product)');
$stmt->param(':userreference', $post['userreference']);
$stmt->param(':claimantpostcode', $post['claimantpostcode']);
$stmt->param(':orderno', $post['orderno']);
$stmt->param(':email', $post['email']);
$stmt->param(':storename', $post['storename']);
$stmt->param(':storetown', $post['storetown']);
$stmt->param(':dateofsale', $post['dateofsale']);
$stmt->param(':dateofdelivery', $post['dateofdelivery']);
$stmt->param(':acknowledgementno', $post['acknowledgementno']);
$stmt->param(':product', $post['product']);
try {
$stmt->execute();
$this->template->content->post = $post;
$this->template->content->thanks = true;
} catch (Exception $e) {
FB::error($e);
}
}
}
And the other is the actual form, see part of it below:
echo form::label('dateofdelivery', 'Date of Delivery:');
echo form::input('form[dateofdelivery]', $val, array('class'=>'input', 'id'=>'dateofdelivery'));
echo form::label('acknowledgementno', 'Acknowledgement No:');
echo form::input('form[acknowledgementno]', $val, array('class'=>'input', 'id'=>'acknowledgementno'));
echo form::label('product', 'Product:');
echo form::select('form[product]', array(
'' => 'Please select from the list',
'In store' => 'In store',
'Word of mouth' => 'Word of mouth',
'Television' => 'Television',
'Newspaper' => 'Newspaper',
'Magazine' => 'Magazine',
'Internet' => 'Internet',
'Google Reasearch' => 'Google Reasearch',
'Radio' => 'Radio',
'Medical Recommendation' => 'Medical Recommendation',
), '', array('class="select"', 'id'=>'product'));
echo form::submit('btnSubmit', 'Submit', array('id'=>'btnSubmit', 'class'=>'button'));
echo '</div>';
echo '<div class="clearfix"></div>';
echo form::close();
You have some errors in your markup, let's see:
In this line you have some errors:
echo form::select('form[myproduct]', $sql = mysql_query("SELECT description FROM claim_incentive");
You are opening two parenthesis but only closing one should be something like:
echo form::select('form[myproduct]', $sql = mysql_query("SELECT description FROM claim_incentive"));
It's not necessary to assign the sql variable inside the method, any way I don't know which kind of framework are you using, so I am only noticing syntax errors.
Related
I wanted to create a shortcode so I can "connect" my Coppermine gallery with my Wordpress, sadly I haven't been able to do it
I use this in my posts
[cpg album="533"]
To call this function
function cpg_shortcode( $attr ) {
shortcode_atts(
array(
'album' => 1,
), $attr
);
return $album_id = $attr['album'];
return '<script src="http://linklink.net/cpg/api-posts.php"></script>';
}
add_shortcode( 'cpg', 'cpg_shortcode' );
And this is the script file, which has no errors, it work perfectly fine, but I have to get the album id in it
$query = mysql_query("SELECT * FROM `cpgq7_pictures` WHERE aid=$album_id ORDER BY ctime DESC LIMIT 0 , 3");
echo 'document.write(\'';
if(mysql_num_rows($query) == 0){
echo 'No hay fotos';
} else {
echo '<h6>';
while($row = mysql_fetch_array($query)){
$domain = "http://linklink.net/cpg";
$album_url = "$domain/thumbnails.php?album=$album_id#content";
$album_img = "$domain/albums/".$row['filepath'].'thumb_'.$row['filename'];
echo '<img src="'.$album_img.'" alt="" />';
}
echo '<img src="https://i.imgur.com/4wmomUt.png" alt="" /></h6>';
}
echo '\');';
When I try to get the album id from the shortcode it doesn't work
Any help is appreciated.
I copy/pasted your shortcode and this line works as intended:
return $album_id = $attr['album'];
Returns the passed album parameter. If you want, you can use extract to have the id directly as $album available:
extract(shortcode_atts(
array(
'album' => 1,
)
, $attr));
now this looks pretty much wrong:
<script src="http://linklink.net/cpg/api-posts.php"></script>
is for javascript, it has nothing to do with php. just include the sql statement and output directly in your shortcode. changed the way of returning the data (ob_start/get_clean). also, like Dharman mentioned, check out how to execute sql statements safely.
function cpg_shortcode($attr) {
extract(shortcode_atts(
array(
'album' => 1,
)
, $attr));
ob_start();
$query = mysql_query("SELECT * FROM `cpgq7_pictures` WHERE aid=$album ORDER BY ctime DESC LIMIT 0 , 3");
if (mysql_num_rows($query) == 0) {
echo 'No hay fotos';
} else {
echo '<h6>';
while ($row = mysql_fetch_array($query)) {
$domain = "http://linklink.net/cpg";
$album_url = "$domain/thumbnails.php?album=$album#content";
$album_img = "$domain/albums/" . $row['filepath'] . 'thumb_' . $row['filename'];
echo '<img src="' . $album_img . '" alt="" />';
}
echo '<img src="https://i.imgur.com/4wmomUt.png" alt="" /></h6>';
}
return ob_get_clean();
}
add_shortcode('cpg', 'cpg_shortcode');
I'm taking values from MySQL(PHPMyAdmin) to PHP and after that, I send them to an echo for taking those values on Ionic. I don't know why but when I add a specific value (Bio field from MySQL) inside the array and execute the .php file thows me a white screen.
Here is my code:
json-tecnico.php
<?php
require_once('connect.php');
$consulta = "SELECT
idTecnico,
nombre,
apellido,
cedula,
genero,
telefono,
usuario,
correo,
ubicacion,
bio,
rate,
catg.descripcion Categoria_Descripcion,
subcat.descripcion Subcategoria_Descripcion
FROM
tecnico AS tec
INNER JOIN categoria AS catg
ON
tec.categoria = catg.idCategoria
INNER JOIN subcategoria AS subcat
ON
subcat.idCategoria = catg.idCategoria AND subcat.idSubcategoria = tec.subcategoria
WHERE categoria = '".$_GET['categoria']."'";
if(isset($_GET['subcategoria'])){
$consulta = $consulta . " AND subcategoria = '".$_GET['subcategoria']."';";
}
$resultado = mysqli_query($conexion, $consulta);
while($columna = mysqli_fetch_assoc($resultado)){
$data[] = array(
'id' => $columna['idTecnico'],
'nombre' => $columna['nombre'],
'apellido' => $columna['apellido'],
'cedula' => $columna['cedula'],
'genero' => $columna['genero'],
'usuario' => $columna['usuario'],
'ubicacion' => $columna['ubicacion'],
'bio' => $columna['bio'],
'rate' => $columna['rate'],
'subcategoria' => $columna['Subcategoria_Descripcion']
);
}
if(!empty($data)){
echo json_encode($data);
}
else{
$data[] = array(
'success' => 'No se encontro usuarios con el criterio de busqueda dado',
'workers' => false
);
echo json_encode($data);
}
mysqli_close($conexion); ?>
The white screen shows when I put the '007' value on $_GET['subcategoria'] and when I put the line 'bio' => $columna['bio'].
On MySQL, bio is varchar(500). Why is this happening?
A photo of the "tecnico" table on PHPmyAdmin
Well, I already dig into the problem, just was an Encoding error.
Using utf8_encode() on all the String variables solved my problem.
You can use this if you're getting encoding problems:
I got the Answer from here!
function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} elseif (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}
and after that:
echo json_encode(utf8ize($data));
I need to create a drcode using the last insert id in the prescribed format,
previously I have used cor php to get the code but now in codeigniter am not able to get the code as the previous one. How can i do this? I am providing my controller and model
Controller
public function newdoctor_post() {
$employee_id = $this->post('EMPLOYEE_ID');
$doctorname = $this->post('DOCTOR_NAME');
$mobilenumber = $this->post('DRMOBILE');
$users_data = $this->Rest_user_model->doctor_exists($mobilenumber);
if ($users_data == 1) {
$message = ['status' => 2,
// 'result' => array(),
'message' => 'Doctor already registered'];
} else {
$speciality = $this->post('SPECIALITY');
$longitude = $this->post('LONGITUDE');
$latitude = $this->post('LATITUDE');
$drcode = $this->post('DRCODE');
$createdon = date('Y-m-d H:i:s');
$insert_array = array('EMPLOYEE_ID' => $employee_id, 'DOCTOR_NAME' => $doctorname, 'DRMOBILE' => $mobilenumber, 'SPECIALITY' => $speciality, 'LONGITUDE' => $longitude, 'LATITUDE' => $latitude, 'CREATEDON' => $createdon);
$users_data = $this->Rest_user_model->doctorregistration($insert_array);
$message = ['status' => 1, // 'result' => $users_data,
'message' => 'Doctor Registered Successfully'];
}
$this->set_response($message, REST_Controller::HTTP_OK);
}
Model
public function doctorregistration($data) {
if (!empty($data)) {
$this->db->insert('DOCTORDETAILS', $data);
return $this->db->insert_id();
}
return 0;
}
Code Generation
$sql1="SELECT DRCODE FROM DOCTORDETAILS ORDER BY DRCODEDESC LIMIT 1";
$query=mysql_query($sql1);
if (!$sql1) { // add this check.
die('Invalid query: ' . mysql_error());
}
$output_array2=array();
while($row=mysql_fetch_assoc($query))
{
$ids=$row['DRCODE'];
}
// echo $ids;
if($ids){
$su=1;
$num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
$unique=$num;
}else{
$unique='DR000001';
}
Try this - replace this code below with your Code Generation code.
$sql1="SELECT DRCODE FROM DOCTORDETAILS ORDER BY DRCODEDESC LIMIT 1";
$query=$this->db->query($sql1);
if (!$sql1) { // add this check.
die('Invalid query');
}
$output_array2=array();
foreach($query->result_array() as $row)
{
$ids=$row['DRCODE'];
}
// echo $ids;
if($ids){
$su=1;
$num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
$unique=$num;
}else{
$unique='DR000001';
}
You should make a different column fr drcode, leave it blank.
Now make a trigger on insert, it should be run after insert.
I am assuming DOCTORDETAILS as table name and drcode as column name
DELIMITER $$
CREATE TRIGGER trigger_after_insert
AFTER INSERT ON `DOCTORDETAILS` FOR EACH ROW
begin
UPDATE `DOCTORDETAILS` set drcode=CONCAT('DR',new.id);
END$$
DELIMITER ;
The trigger create a new string with your new auto generated id
Trigger in MySQL:
TechonTheNet
MySQL Trigger on after insert
I've created php ekart site just for testing purpose. My query is when I click on add to cart it should firstly validate whether any one color and any one size is selected, if both have been selected it adds product to the cart this works fine but if I add same product but with different color or size it overlaps the same product which was already added and that products quantity turns to 1 but if I select another product it works well but same thing goes when I select different size or color. Since I am a beginner at php, I'm not sure where am I going wrong here.
Php code of Product add to cart is given Below:
$sql = "SELECT * FROM allprods WHERE listing = '$listing' and id = '$id'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 1)
{
$row = mysql_fetch_array($data);
if (isset($_SESSION['cart'][$id]))
{
if(isset($_SESSION['cart'][$id][$color]) && isset($_SESSION['cart'][$id][$size]))
{
$_SESSION['cart'][$id]['quantity']++;
echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
}
else
{
$_SESSION['cart'][$id] = array('quantity' = >1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
}
}
else
{
$_SESSION['cart'][$id] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
}
}
Cart program code
<?php
if(isset($_POST['qty']))
{
foreach($_POST['qty'] as $product_id=>$item_qty)
{
$id=(int)$product_id;
$qty=(int)$item_qty;
if($qty==0)
{
unset($_SESSION['cart'][$id]);
}
elseif($qty>0)
{
$_SESSION['cart'][$id]['quantity']=$qty;
}
}
}
else
{
echo "";
}
if(!empty($_SESSION['cart']))
{
require "../link_db.php";
$sql="SELECT * FROM allprods WHERE id IN(";
foreach($_SESSION['cart'] as $id=>$value)
{
$sql.=$id.',';
}
$sql=substr($sql,0,-1).') ORDER BY product_id ASC';
$data=mysql_query($sql);
while($row=mysql_fetch_array($data))
{
?>
Table row and data goes here!!
<?php
}
else
{
?>
<tr>
<th colspan='4' class='noprodavailablewrap'>
There are no products in your cart.
</th>
</tr>
<?php
}
?>
DB structure given below:
Use this code
<?php
$sql = "SELECT * FROM allprods WHERE listing = '$listing' and id = '$id'";
$data = mysql_query($sql);
if (mysql_num_rows($data) == 1)
{
$row = mysql_fetch_array($data);
$index = md5($id.$color.$size);
if( isset($_SESSION['cart'][$index]) && isset($_SESSION['cart'][$index]['color']) && $_SESSION['cart'][$index]['color'] == $color && isset($_SESSION['cart'][$index]['size']) && $_SESSION['cart'][$index]['size'] == $size){
$_SESSION['cart'][$index]['quantity']++;
echo "<script>alert('".$row['product_name']." has been added to your cart.');</script>";
}else{
$_SESSION['cart'][$index] = array('quantity' => 1, 'price' => $row['product_special_price'], 'cat' => $cat, 'id' => $id, 'size' => $size, 'color' => $color, 'name' => $row['product_name']);
echo "<script>alert('" . $row['product_name'] . " has been added to your cart.');</script>";
}
}
?>
I am new to SugarCRM. I've created a custom field named 'account name' in the Meetings module so that if we select Contacts from related to field, the 'account name' of that Contact is automatically added to the field.
Here's my code:
$hook_array['after_retrieve'] = Array();
$hook_array['after_retrieve'][] = Array(1, 'Add custom account',
'custom/modules/Meetings/AddAccount.php','AddAccount', 'addAcc');
LogicHook:
class AddAccount
{
public function addAcc(&$bean, $event, $arguments)
{
global $current_user;
global $db;
echo "<pre>";
$meeting_id = $_REQUEST['record'];
$query = "SELECT * FROM `meetings_contacts` WHERE `meeting_id` LIKE '$meeting_id'";
$result = $bean->db->query($query, true, " Error filling in additional detail fields: ");
if ($bean->db->getRowCount($result) > 0) {
while ($row = $bean->db->fetchByAssoc($result)) {
$contact_id = $row['contact_id'];
}
if (isset($contact_id)) {
$query1 = "SELECT * FROM `accounts_contacts` WHERE `contact_id` LIKE '$contact_id'";
$result1 = $bean->db->query($query1, true, " Error filling in additional detail fields: ");
while ($row1 = $bean->db->fetchByAssoc($result1)) {
$account_id = $row1['account_id'];
}
$query2 = "SELECT * FROM `accounts` WHERE `id` LIKE '$account_id'";
$result2 = $bean->db->query($query2, true, " Error filling in additional detail fields: ");
while ($row2 = $bean->db->fetchByAssoc($result2)) {
$account_name = $row2['name'];
}
$update_custom_account = "UPDATE `meetings_cstm` SET `accountname_c` = '$account_name' WHERE `meetings_cstm`.`id_c` = '$meeting_id';";
$Change = $bean->db->query($update_custom_account);
}
}
}
}
The problem is that the field is getting added but the "i" in the ListView has stopped working. Is there a simpler way than this long query?
Thanks in advance.
This is a better way of doing the above.
custom/modules/Meetings/logic_hooks.php
// position, file, function
$hook_array['after_retrieve'] = Array();
$hook_array['after_retrieve'][] = Array(1, 'Add custom account', 'custom/modules/Meetings/AddAccount.php', 'AddAccount', 'getAccountName');
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'Add custom account', 'custom/modules/Meetings/AddAccount.php', 'AddAccount', 'getAccountName');
custom/modules/Meetings/AddAccount.php
class AddAccount {
public function getAccountName(&$bean, $event, $arguments) {
if ($bean->parent_type == 'Contacts') {
$contact = BeanFactory::getBean('Contacts', $bean->parent_id);
$contact->load_relationship('accounts_contacts');
$account = BeanFactory::getBean('Accounts', $contact->account_id);
$bean->account_name_c = $account->name;
}
}
}
This way, you are using the bean and not SQL.
EDIT:
To add the new field, you can create this fileā¦
custom/Extension/modules/Meetings/Ext/Vardefs/account_name_c.php
<?php
$dictionary['Meeting']['fields']['account_name_c'] =
array (
'name' => 'account_name_c',
'vname' => 'LBL_ACCOUNT_NAME_C',
'type' => 'varchar',
'len' => '255',
'unified_search' => true,
'comment' => 'Account name for meeting',
'studio' => 'true',
);
Then after a Repair/Rebuild, go to Studio > Meetings > Layouts > ListView and drag/drop the new field from 'Hidden' to 'Default.' Select the 'Save & Deploy' button, and after saving the Meeting record, your account name will appear in the list view.