I am facing the problem that when I try to update the amount field in table using query in php code then it is not working. See this image:
But if i did this removing by the financial year field then it is working......
My php code is:
if(isset($_POST['mid_update']))
{
echo $subscription3=$_POST['subscription'];
echo $member_type3=$_POST['member_type'];
echo $financial_year3=$_POST['financial_year'];
echo $amount3=$_POST['amount'];
$qry=mysqli_query($con,"update dbo_tbfeemaster set nu_amount='$amount3' where nu_sub_id='$subscription3' and vc_member_type='$member_type3' and vc_financial_year='$financial_year3'");
if($qry)
{
header("location:subscription_fee_master.php?w=updation success");
}
else
{
header("location:subscription_fee_master.php?w=updation not success");
}
}
With this code the updation is successful showing but table is not updating.
I change date format as:
<option value="<?php $dat1=$q['dt_period_start_date'];
echo date("d-M-Y",strtotime($dat1));?> to <?php $dat2=$q['dt_period_end_date'];
echo date("d-M-Y",strtotime($dat2));?>">
And is equal to the same as database financial year column...
Everthing is going fine then why my table is not updating. Any suggestion would be highly appreciated and feel free to ask me if i forget something to mention here regarding my problem...
It looks like the date formats you use in the query don't line up. Try something like this to make sure the formats are the same. You might want to split those two dates up and store them in separate columns. Which would make this a lot easier imho.
$dateStr = '2011-04-01 to 2012-03-31';
$parts = explode('to', $dateStr);
$result = array_map(function($value) {
$date = date_create(trim($value));
return date_format($date, 'd-M-Y');
}, $parts);
$dateStr = implode(' to ', $result);
Related
Already solved. I just used WHERE MONTH(due_date) = $month in the SQL clause. Never knew it would just be like that. Thank you for all your answer!
We have a table called bills. We do not delete a bill even if it is paid already for record purposes.
So our goal is to only display The Bills for this Month. I have a $cur_month = current month value. I know how to extract the month value from a field using MONTH(), using a loop to run though the table, but when I try to echo MONTH(date) the value through out the displayed series is just the MONTH VALUE of the very first row. It seems it failed to get the MONTH VALUE of the other rows.
Fixed code below
$query = "SELECT * FROM bill WHERE MONTH(due_date)=$month";
$bresult = mysql_query($query);
while($brow = mysql_fetch_array($bresult, MYSQL_ASSOC))
{
$bdata = mysql_fetch_assoc(mysql_query("SELECT MONTH(due_date) AS M FROM `bill`"));
if($bdata['M'] == $month)
{
echo "<tr>";
echo "<td>".$brow['room_id']."</td>";
echo "<td>".$brow['tenant_id']."</td>";
echo "<td>".$brow['due_date']."</td>";
echo "</tr>";
}
}
$month there is the holder of the current month
$bdata['M'] there is the holder of the month extracted. We just displayed it to check.
So if extracted_month is equls to current_month then display bill
I hope you can help me in this.
PS: Still an amateur. This is not yet an online website. We only need help for the purpose of having it work.
1) Use a WHERE statement in your first SQL to only fetch those rows from the table.
Like this:
$query = "SELECT *, MONTH(due_date) as M FROM bill WHERE MONTH(due_date)=" . $month;
$bresult = mysql_query($query);
while($brow = mysql_fetch_array($bresult, MYSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$brow['M']."</td>";
echo "<td>".$brow['tenant_id']."</td>";
echo "<td>".$brow['due_date']."</td>";
echo "</tr>";
}
2) I find it good practice to always check if the result object is created and if so, to check if it returned matches (with mysql_num_rows($result)). That way you can show an error if something goes wrong (most likely in the SQL statement) or show the user that there are no matches (bills in this case).
3) Try to use MYSQLI to connect to your database instead of MYSQL, since the latter is deprecated. (See: http://php.net/manual/en/mysqli-result.fetch-assoc.php for an example.)
Try solving this problem using SQL.
SELECT b.amount_paid
FROM bills b
WHERE MONTH(b.due_date) = 3
Using the result of this query you would have all of the amounts for this month. Sum your result and you are done.
newbie here at a small non-profit. I inherited a website written long ago and trying to update it. I need help. This page is to insert hyperlinks to forms for entering monthly data. Until now the website was automatically populated with current month and year hyperlinks. Now it is stuck on April 2014. I don't see anywhere in the Mysql tables or code to add the new months. I believe it may be the 'function getNewMonth($x)' but I don't know why it stopped working. I need to get May in there!
Thanks in advance.
function getNewMonth($x)
{
global $database_fvca, $fvca, $myid, $legacyprint;
mysql_select_db($database_fvca, $fvca);
$sql = "SELECT instid FROM instance WHERE reportyear=".date("Y", $x)." AND reportmonth=".date("n", $x).
" AND orgid=".$myid;
$q = mysql_query($sql);
$n = mysql_num_rows($q);
if ($n == 0)
{
return '<td><a href="monthstart.php?orgid='.$myid.'&month='.date("n", $x).'&year='.
date("Y", $x).'">begin</a></td><td> </td>';
}
else
{
$a = mysql_result($q,0);
return '<td>edit</td><td><a href="viewmonth.php?instid='.$a.
'">view report</a></td>'; } }
function getMonth($date)
{
I can insert data into the database using CodeIgniter and also get the data from the database, but the problem is I can't get the data from date 1-9 (the data is present in the database). After the 9th of the month, the data is retrieved successfully.
Controller:
function display($year=null,$month=null){
if (!$year) {
$year = date('Y');
}
if (!$month) {
$month = date('m');
}
$this->load->model('Calendar_model');
if ($day = $this->input->post('day')) {
$this->Calendar_model->add_calendar_data("$year-$month-$day",$this->input->post('data')
);
}
$this->load->model('calendar_model');
$data['calendar']=$this->calendar_model->generate($year,$month);
$data['viewName']=('Student/s_calendar');
$this->load->view('Student/template',$data);
}
Model:
function get_cal_data($year,$month){
$query=$this->db->select('date,data')->from('student_calender')->like('date',"$year-$month")->get();
$cal_data=array();
foreach($query->result() as $row){
$cal_data[substr($row->date,8,2)]=$row->data;
}
return $cal_data;
}
use this function for your calendar data the codeignitor only picks values past 9 because it compares the two characters in the html page to the two characters of days from your database. any number below 10 does not match because in your database it saves any day below 10 with a zero. use this
function get_calender_data($year,$month)
{
$query = $this->db->select('date, data')->from('calendar')
->like('date',"$year-$month",'after')->get();
$cal_data = array();
foreach ($query->result() as $row) {
if(substr($row->date,8,1)==0)
{
$cal_data[substr($row->date,9,1)] = $row->data;
}
else{
$cal_data[substr($row->date,8,2)] = $row->data;
}
}
return $cal_data;
}
I had the same problem via zend framework finally I got the point that the data in mysql is in this format :
YYYY-MM-DD HH:ii:ss
And my query was in this format :
YYYY-m-d h:i:s
It means for example I hade
2013-01-02 13:02:30
in database and I searched for time between
2013-1-2 13:2:30 and 2013-1-3 13:2:30
which gave me wrong answers.
use vardump and see your values of year and month!
I'm building a calendar and I'm really dumb when it comes to arrays.
First of all, I need to query my database, so:
$events = mysql_query ("SELECT id,title,date WHERE date BETWEEN 2009-01-01 AND 2009-01-31")
or die(mysql_error());
So, now I need to order those events so, when I'm echoing my calendar table, I can check this array for events.
In this great Calendar written by David Walsh, he does a query for every day, but I suppose it would be a performance nightmare.
So...any ideas how can I do this?
First up, make sure you're comparing dates with dates. I think your code should be (i.e. with single quotes around date):
$events = mysql_query ("SELECT id,title,date WHERE date
BETWEEN '2009-01-01' AND '2009-01-31' order by date asc")or die(mysql_error());
assuming date is a date column.
i would do that in a elegant for loop
for($i=0;$i<30;$i++)
{
$events[] = mysql_fetch_array(mysql_query ("SELECT id,title,date WHERE date BETWEEN 2009-01-01 AND 2009-01-{$i}"));
}
now you have an array do somethingg with it like this
for($i=0;$i<count($events);$i++)
{
echo $events["id"];
}
will return each id for events in that array
Ok regarding your comments you can do something like this: Get the events like Davek proposed (note the ORDER BY!)
$events = mysql_fetch_assoc(mysql_query ("SELECT id,title,date WHERE date BETWEEN '2009-01-01' AND '2009-01-31' ORDER BY date asc"));
Then you got the events ordered by the date. To output it you can do this:
$last_date = null;
foreach($event in $events) {
if($last_date !== $event['date']) {
echo 'Events for day ' . $event['date'] . ": \n";
$last_date = $event['date'];
}
echo $event['title'] . "\n"
}
Note: This is just a rough sketch, you have to adjust the output of course, but it should give you the right idea.
Output would look like this (in my example):
Events for 2009-01-01:
Event 1
Event 2
Events for 2009-01-02:
Event 1
.
.
.
Edit after comment:
You can write your own function:
function get_events($date, $events) {
$result = array();
foreach($event in $events) {
if($event['date'] == $date) {
$result[] = $event;
}
}
return $result;
}
But this way you search the the complete array over and over again for each day. You can improve it, if you remove the events you already searched for from the $events array. So every time you search in a smaller array. But I would only do this if there is a performance issue.
Basically I have articles in my database and I want to alter the way the first record displays. I want the lastest (Posted) article to be the focus and the older article just to list, (see F1.com). I need to know how to get the first of my values in the array and get it to display differently but I am not sure how to do this, I can do it so all rows display the same just not how to alter the first row. I also need to know how to tell the rest of the rows to display the same afterwards im guessing you use an if statement there and before that some kind of count for the rows.
Current code:
$result = mysql_query("SELECT * FROM dbArticle WHERE userID='".$_SESSION["**"]."' ORDER BY timestamp DESC");
while($row = mysql_fetch_array($result))
{
echo "<h2 class=\"heading1\">". $row['title'] ."</h2>";
echo "By: ".$row['username']." Type: ".$row['type']." Posted: ".$row['timestamp']."
$body = $row['body'];
echo "<br/><p>";
echo substr("$body",0,260);
echo "...<span class=\"tool\"><a class=\"blue\" href=\"index.php?pageContent=readArticle&id=".$row['id']."\">Read More</a></span></p><hr/>";
}
mysql_close($con);
Ok I have taken Luke Dennis's code and tried to test it, but I am getting this error: Warning: Invalid argument supplied for foreach() this is the line of the foreach statment. Something that has just come to mind is that I will only want 5 or so of the older articles to display. This is what I have thats creating the error:
<? $con = mysql_connect("localhost","****","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("******", $con);
$result = mysql_query("SELECT * FROM dbArticle ORDER BY timestamp DESC");
$first = true;
foreach($result as $row){
if($first)
{
echo"".$row['title']."";
echo"this is the headline";
$first = false;
}
else
{
echo"".$row['title']."";
}
}
?>
Do I need to add mysql_fetch_array somewhere to set the array up?
I would just iterate through the results and apply a css class to the first entry:
$first = true;
while ($row = mysql_fetch_assoc($result)) {
$cssClass = '';
if ($first) {
$cssClass = 'highlight';
}
echo '<p class="' . $cssClass . '">' . $row['text'] . '</p>';
$first = false;
}
It's a bit crude, but I often hard-code a variable to designate the first run through a loop. So something like:
$first = true;
foreach($list_of_items as $item)
{
if($first)
{
// Do some stuff
$first = false;
}
else
{
// Do some other stuff
}
}
A simple if statement when looping through your results will usually do the trick. You can use a boolean to indicate if you've output the first row of results or now. If you haven't then give it a particular style and then set the boolean to true. Then all subsequent rows get a different style.
All of the above are correct. Luke Dennis' post is of course fleshed-out a bit more.
As Brian Fisher said, add some CSS styling to the first link when you encounter it per Luke's post.
I took a look at the article list on the F1 website. Pretty well constructed site - "One would expect that." :-)
Anyway, the article listings are contained within a two row table (summary="Latest Headlines") in descending order (newest first).
Just place a class in the second column (<td class="first-news-article">). Then add the class name and appropriate styling values in the css file - probably your' modules.css. There's already quite a few class values associated with articles in that file, so you may be able to just use an existing value.
That should be about it - other than actually doing it!
By the way, judging by the quality of the underlying html, I'm assuming there's already an "article list emitter." Just find that emitter and place the appropriate conditional to test for the first record.
Darrell
I just noted your code addition. I assume that you were showing the F1 site as an example. Anyway, I think you're on your way.
I presume you have some code that loops through your resultset and prints them into the page? Could you paste this code in, and that might give us a starting point to help you.
I don't know PHP, so I'll pseudocode it in Perl. I wouldn't do it like this:
my $row_num = 0;
for my $row ($query->next) {
$row_num++;
if( $row_num == 1 ) {
...format the first row...
}
else {
...format everything else...
}
}
The if statement inside the loop unnecessarily clutters the loop logic. It's not a performance issue, it's a code readability and maintainability issue. That sort of thing just BEGS for a bug. Take advantage of the fact that it's the first thing in the array. It's two different things, do them in two different pieces of code.
my $first = $query->next;
...format $first...
for my $row ($query->next) {
...format the row...
}
Of course, you must make the first row stand out by using tags.
I'd use array_shift():
$result = mysql_fetch_assoc($resultFromSql); // <- edit
$first = array_shift($result);
echo '<h1>'.$first['title'].'</h1>';
foreach ($result as $row) {
echo '<h2>'.$row['title'].'</h2>';
}
The best way to do this is to put a fetch statement prior to the while loop.
Putting a test inside the while loop that is only true for one iteration can be a waste of time for a result of millions of rows.