How to fectch data from table in cakephp - mysql

Hi i have one table in my database which has list of states and i want to fetch this data from the table but my query is not executing properly it gives me some error
<?php
require_once('../Config/database.php');
$result1=$this->Signup->query("SELECT * FROM states");
//echo $popular;
while($post = mysql_fetch_array($result1))
{ ?>
<table width="380">
<tr>
<td class="table_txt"><a class="thickbox tn" href="demo.php?state_name=<?php echo $post['state_name']?>&state_id=<?php echo $post['state_id']?>&height=430&height=430&width=700&inlineId=myOnPageContent"><?php echo $post['state_name']?></a></td>
</tr>
</table>
<?php }
?>
But it gives me error
Warning (512): Method SignupHelper::query does not exist [CORE\Cake\View\Helper.php, line 192
Warning (2): mysql_fetch_array() expects parameter 1 to be resource, null given

Please read the documentation first.
It seems you are trying to get the states, inside the View, with a query.
You need to separate the view from the model.
Create a State model.
Use something like this in your controller:
$this->loadModel('State');
$states = $this->State->find('list'); // this will create a key => value array with the IDs and names
$this->set('states', $states);
In your view, use
<table width="380">
<tr>
<?php foreach ($states as $stateId => $stateName) {
<td class="table_txt"><a class="thickbox tn" href="demo.php?state_name=<?php echo $stateName?>&state_id=<?php echo $stateId?>&height=430&height=430&width=700&inlineId=myOnPageContent"><?php echo $stateName ?>></a></td>
<?php } ?>
</tr>
You might still need some changes, but this is the main idea.

Related

how can I do reverse geolocation using mysql when all values are in the same table?

this is my first question on stackoverflow.
I have a mysql table on my website that list earthquake origins, they all have latitudes and longitudes in the same table.
An example:
Date | Time | Latitude | Longitude | Depth | magnitude | Magnitude type
2017/04/03 | 03:08:37.20 | -26.762 | 26.627 | 261.0 | 3.9 | WFEO
How can I add a reverse geolocation field in the same table where an approximate location name will be displayed?
I thought of downloading the Geonames database, and then putting that in a different table, and then querying the second table, but I don't know.
The mysql server is on a remote webserver.
Note: I am looking for a solution that will do each and every row in the table.
Thanks, I am new to mysql, so I don't have much knolledge in the field.
Any help would be appreciated.
What I've done so far.
I have a csv file which the earthquake parameters get recorded in, I've managed to put that in a mysql database to display as an html table. The code follows:
<?php
$db = new mysqli('localhost:3306','user','password','database');
if ($db->connect_errno) {
echo "Failed to connect to MySQL: " . $db->connect_error;
exit();
}
?>
<table align="center" width="800" border="1" style="border-collapse:collapse; border:1px solid #ddd;" cellpadding="5" cellspacing="0">
<thead>
<tr bgcolor="#FFCC00">
<th>Date</th>
<th>Time</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Depth</th>
<th>magnitude</th>
<th>magnitude type</th>
</tr>
</thead>
<tbody>
<?php
if(($handle = fopen("norcsv.txt", "r")) !== FALSE){
$n = 1;
while(($row = fgetcsv($handle)) !== FALSE){
$db->query('INSERT INTO `events`(`date`, `time`, `latitude`, `longitude`, `depth`, `magnitude`, `magtype`) VALUES ("'.$row[1].'","'.$row[2].'","'.$row[3].'","'.$row[4].'","'.$row[5].'","'.$row[6].'")');
if($n>1){
?>
<tr>
<td><?php echo $row[0];?></td>
<td><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
<td><?php echo $row[4];?></td>
<td><?php echo $row[5];?></td>
<td><?php echo $row[6];?></td>
</tr>
<?php
}
$n++;
}
fclose($handle);
}
?>
</tbody>
</table>
View last seismic event location on a map.
So as you can see, everything works, now I want to add a placename field in the current existing table to use the latitudes and longitudes to compute a geographical placename or maybe distance to placename.
Hope my question is clearer now.
Thanks everyone.

numbering in column and the other column get data from database

I wish to do something like this:
apple
bag
cat
dog
the first column: generate auto number start with 1.
the second column: get data from database
<table>
<?php do { ?>
<tr>
<td><ol>
<li></td>
<td><?php echo $row["object"]; ?></td>
</li></ol>
</tr>
<?php } while ($row = mysql_fetch_assoc($recordset)); ?>
</table>
The coding above make like:
1 apple
1 bag
1 cat
1 dog
I do not know where am I wrong. Please correct me. Thank you.
You're creating a new ordered list with each record. You're also mixing your ordered list in tables, which doesn't make sense - do one or the other.
Maybe something like:
<ol>
<?php do { ?>
<li><?php echo $row["object"]; ?></li>
<?php } while ($row = mysql_fetch_assoc($recordset)); ?>
</ol>

cakephp mysql error

I'm having some trouble with my site. What I want this view to do is display all invoices that relates to a userid(in another table), Instead the code is printing out all the invoices in the one view(disregarding the user id). When looking at the sql statement that cakephp debug spits out it shows the where as a blank(don't worry I'll include the actual sql statment). I also have session code created but I am unsure how to code it so that the mysql data will say where userid = current user;
here is the code for the view
<table width="100%" border="1">
<table width="100%" border="1">
<tr>
<th>Biller</th>
<th>Subject</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php foreach($invoices as $invoice):?>
<tr> debug($invoices);
<td align='center'><?php echo $this->Html->link($invoice['Invoice']['biller'],
array('action' => 'viewinvoice', $invoice['Invoice']['id'])); ;?> </td>
<td align='center'><?php echo $invoice['Invoice']['subject']; ?></td>
<td align='center'><?php echo $invoice['Invoice']['datecreated']; ?></td>
<td align='center'><button>View Invoice</button><button>Dispute Invoice</button></td>
</tr>
<?php endforeach; ?>
</table>
here is the code in the class relating to this view
public function payinvoice($id = null){
$this->set('title_for_layout', 'Pay Invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->layout='home_layout';
$this->set('invoices', $this->Invoice->find('all' , array('conditions' => array('Invoice.biller' => $id))));
}
and here is the sql code that site is using to retrieve the data
SELECT `Invoice`.`id`, `Invoice`.`to`, `Invoice`.`biller`, `Invoice`.`subject`, `Invoice`.`description`, `Invoice`.`amount`, `Invoice`.`datecreated`, `Invoice`.`duedate` FROM `pra_cake`.`invoices` AS `Invoice` WHERE `Invoice`.`biller` IS NULL
It looks like you are not passing an id into your page. The url should look like:
/invoices/payinvoice/2
This is probably the issue of the url
try this
<?php echo $this->Html->link('Invoice',
array('controller' => 'invoices',
'action' => 'payinvoice',
$invoice['Invoice']['id'])
);?>
it will generate
Invoice

HTML form with Zend Form filters/validators

I'm creating a simple contact form within my ZF application. It doesn't feel like it's worth the trouble to manipulate decorators for only a few form elements.
My Question is: am I able to still use Zend Form Filters on elements that are not created with Zend Form:
For Example:
The Form:
<!-- Standard HTML - not generated with ZF -->
<form id="contact-form" method="post" action="/contact/submit">
<input type="text" name="name" />
<input type="email" name="email" />
<input type="submit" name="submit" />
</form>
The Controller:
public function submitAction()
{
$params = $this->_request->getParams();
//Am I able to apply filters/validators to the data I get from the request?
//What is the best way to handle this?
}
I took a look at this (the answer from Darcy Hastings) - and it seems like it would work, it just feels a bit hacky.
Any and all advice appreciated.
Thanks,
Ken
yes, you can use Zend_Filter_Input, here is an example of how to set it up.
//set filters and validators for Zend_Filter_Input
$filters = array(
'trackid' => array('HtmlEntities', 'StripTags')
);
$validators = array(
'trackid' => array('NotEmpty', 'Int')
);
//assign Input
$input = new Zend_Filter_Input($filters, $validators);
$input->setData($this->getRequest()->getParams());
//check input is valid and is specifically posted as 'Delete Selected'
if ($input->isValid()) {
also you may consider using the viewscript decorator to render a Zend Form, The control is absolute (or almost).:
//in your controller action
public function indexAction() {
//a normally constructed Zend_Form
$form = new Member_Form_Bid();
$form->setAction('/member/bid/preference');
//attach a partial to display the form, this is the decrator
$form->setDecorators(array(
array('ViewScript', array('viewScript' => '_bidForm.phtml'))
));
$this->view->form = $form;
//the view
<?php echo $this->form?>
//the partial
//use a normal Zend_Form and display only the parts you want
//processing in an action is done like any other Zend_Form
form action="<?php echo $this->element->getAction() ?>"
method="<?php echo $this->element->getMethod() ?>">
<table id="sort">
<tr>
<th colspan="2">Sort By Shift</th>
<th colspan="2">Sort By Days Off</th>
<th colspan="2">Sort By Bid Location</th>
</tr>
<tr></tr>
<tr>
<td class="label"><?php echo $this->element->shift->renderLabel() ?></td>
<td class="element"><?php echo $this->element->shift->renderViewHelper() ?></td>
<td class="label"><?php echo $this->element->weekend->renderLabel() ?></td>
<td class="element"><?php echo $this->element->weekend->renderViewHelper() ?></td>
<td class="label"><?php echo $this->element->bidlocation->renderLabel() ?></td>
<td class="element"><?php echo $this->element->bidlocation->renderViewHelper() ?></td>
</tr>
<tr></tr>
<tr>
<td colspan="6" style="text-align: center"><?php echo $this->element->submit ?></td>
</tr>
</table>
</form>
Yes, you definitely can use Zend_Form on self-rendered forms.
You can do this in two ways:
Use a Zend_Form object, but don't render it. You create a Zend_Form instance as usual, with all the elements named correctly and attach validators and filters as per normal. In your action, you can then check the form's isValid() and use getValues() to ensure that you collect the filtered data.
The second option is to use Zend_Filter_Input which is a chain of validators and filters. You set up your validators and filters at construction and then call setData to populate the filter with the information from the request. Again, you have isValid() to test and then you use getUnescaped() to retrieve the data. The manual page has more details.

WordPress - Display Post Content and Post Meta on a Page in the Admin Area

I am trying to create a page in my WordPress Admin Area that displays excerpts of posts and various custom field meta in a table-style layout.
If this were a front-end WordPress Template, I could do this very easily using a WordPress Loop and Query, however, I am not so sure how I would go about doing this on a page in the admin area.
Would it be the same, or would I need to use a completely new method? If so, could someone please provide a working example of how I would do this?
The admin page will be created using an included file within my functions.php - or at least that is the plan at the moment, so I just need help in figuring out how to pull the WordPress Excerpts and Post Meta.
you can use the WP_Query object everytime after WordPress is initialized, so if you like you can even make thousands of nested queries in den WordPress backend if you want to do this.
This is the way to go:
Create an action to add your backend page - write a Plugin or put it into your functions.php
Setup the Menu Page - the code is an example for a full backend administration Page of your Theme
Include your queries using the WP_Query object - optionally make database queries directly (http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query). Possibly use the "widefat" class of WordPress, for pretty formatting.
Make sure that your changes are saved correctly
add_action('admin_menu', 'cis_create_menu');
function cis_create_menu() {
//create new top-level menu
add_menu_page(__('Theme Settings Page',TEXTDOMAIN),__('Configure Theme',TEXTDOMAIN), 'administrator', __FILE__, 'cis_settings_page', '');
//call register settings function
add_action('admin_init','cis_register_settings');
}
function cis_register_settings() {
register_setting('cis-settings-group','cis_options_1','cis_validate_settings');
}
function cis_settings_page() {
// All Text field settings
$op_fields = array(
array(__('Label 1','textdomain'),"Description 1")
);
?>
<div class="wrap">
<h2><?php echo THEME_NAME; _e(": Settings",TEXTDOMAIN); ?></h2>
<?php
settings_errors();
?>
<form method="post" action="options.php">
<?php
settings_fields( 'cis-settings-group' );
$options = get_option('cis_options_1');
?>
<h3><?php _e('General','textdomain'); ?></h3>
<table class="widefat">
<thead>
<tr valign="top">
<th scope="row"><?php _e('Setting','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('Value','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('Description','ultrasimpleshop'); ?></th>
<th scope="row"><?php _e('ID','ultrasimpleshop'); ?></th>
</tr>
</thead>
<tbody>
<?php
// the text-settings we define fast display
$i=1;
foreach($op_fields as $op) {?>
<tr valign="top">
<td><label for="cis_oset_<?php echo $i; ?>"><?php echo $op[0]; ?></label></td>
<td><input size="100" id="cis_oset_<?php echo $i; ?>" name="cis_options_1[cis_oset_<?php echo $i; ?>]" type="text" value="<?php echo esc_attr($options['cis_oset_'.$i]);?>" /></td>
<td class="description"><?php echo $op[1]; ?></td>
<td class="description"><?php echo $i; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes',TEXTDOMAIN) ?>" />
</p>
</form>
</div>
<?php }
// Validate the user input - if nothing to validate, just return
function cis_validate_settings( $input ) {
$valid = array();
$i= 1;
while(isset($input['cis_oset_'.$i])) {
$valid['cis_oset_'.$i] = $input['cis_oset_'.$i];
$i++;
}
$cis_additional_settings = get_option('cis_options_1');
foreach($input as $ikey => $ivalue) {
if($ivalue != $valid[$ikey]) {
add_settings_error(
$ikey, // setting title
"cis_oset_".$ikey, // error ID
str_replace("%s",$ikey,__('Invalid Setting in Settings Area ("%s"). The value was not changed.',TEXTDOMAIN)), // error message
'error' // type of message
);
$valid[$ikey] = $cis_additional_settings[$ikey];
}
}
return $valid;
}
outside the loop you would need to use
$post->post_excerpt
or try this
function get_the_excerpt_here($post_id)
{
global $wpdb;
$query = "SELECT post_excerpt FROM $wpdb->posts WHERE ID = $post_id LIMIT 1";
$result = $wpdb->get_results($query, ARRAY_A);
return $result[0]['post_excerpt'];
}