I'm currently working on my computing project which would allow a user to create an indefinite amount of files. What I'm currently stuck on is retrieving these values to let a user select one here is my code:
function DispMonth(){
$dateList=GetDates($_Session['user']);//use session id
foreach($dateList as $value){//get each value
echo '<tr>' .$value.'</tr>';}
}
?>
<body>
<table>
<th><tr> Made Budgets</tr> </th>
<?php DispMonth(); ?>
</body>
</html>
My GetDates function is:
function GetDates($id){
$result=mysql_query('select monthYear from database where userId='.$_Session['user'].'');
while ($row=mysql_fetch_object($result)){
$x[]=$row['monthYear'];}
return x;
}
Essentially I would like the table to look something like this:
|monthYear| edit | delete |
With edit and delete being links/buttons which would send the value of monthYear to a new php page to fetch all the values.(Monthyear is a primary key in my sql table)
There are few issues with your code which you need to fix, and few changes you need to make to make it work, such as:
Your table structure is wrong. <tr> cannot be inside <th>, also the closing </table> tag is missing.
You're fetching rows from the result set using mysql_fetch_object() function but accessing the column values using $row[...], which is wrong. Use mysql_fetch_array() instead.
while ($row=mysql_fetch_array($result)){ ...
Given the fact that monthYear is the primary key of your table, make use of this column value in edit and delele buttons in your foreach loop, like this:
edit
delete
Later, on newPage.php page, you can edit or delete any particular row like this:
if(isset($_GET['edit'])){
$monthYear = $_GET['monthYear'];
// Edit the row
}
if(isset($_GET['delete'])){
$monthYear = $_GET['monthYear'];
// Delete the row
}
So your functions and table structure would be like this:
<table>
<tr>
<th>Made Budgets</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php DispMonth(); ?>
</table>
function DispMonth(){
$dateList=GetDates($_Session['user']);
foreach($dateList as $value){
?>
<tr>
<td><?php echo $value; ?></td>
<td>edit</td>
<td>delete</td>
</tr>
<?php
}
}
function GetDates($id){
$result=mysql_query('select monthYear from database where userId='.$_SESSION['user'].'');
$x = array();
while ($row=mysql_fetch_array($result)){
$x[] = $row['monthYear'];
}
return x;
}
Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.
Related
I have a webpage where it shows the lists of Projects and the monitoring of its progress/finances for every quarter. As shown below:
As you can see, my table is comprises of Project Name and a lists of sub-title's underneath it. And a series of columns per each quarter. Thru PHP I was able to populate the list of sub-titles under the Project Name, which also being fetched from the server side. Here's the code:
$sql = mysqli_query($con," My SELECT Statement ");
$i=0;
while($row = mysqli_fetch_assoc($sql)){
$ptitle = $row['Title'];
$iname = $row['Item'];
if($i%1)
{
?>
<?php } else { ?>
<tr>
<?php } ?>
<td width="25%"><?php echo $ptitle; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo "<ul style='list-style-type: none;'><li>".nl2br($iname)."</li></ul>"; ?></td>
<td contenteditable="true" name="v1"></td>
Note: ptitle = ProjectName and iname = Semi-title underneath the Project's name.
Now, as you can see, the Project Name column literally "conquer" a single row on the left. Yet, the rows under the column of each quarter, should have its own separately, and must be parallel to the every sub-title underneath the Project Name. (Please refer to the image above for this) the only problem am encountering is... how can I make an editable row from inside a row, without affecting mysqli result? coz basically my table right now is kinda look like this:
Anyone who's more experience on this? I need your help.
PS: ...and oh! You might be wondering why do I include JSON in the title? It is because, I originally use JSON for editing those table rows before I even use mysqli_fetch_array. But when I include the results of the array inside the <table> tag, everything's changed and JSON is no longer working. So as of now, I am force to do it manually, meaning typing each <td contenteditable=true> in all of those rows. Yet, its not the desired output since I need another row within an existing row. Ideas? Anyone?
Figure Two:
Figure Three:
Count the number of lines in $iname, and then use a loop to create that many rows of contenteditable cells. You can also use this in the rowspan attribute of the <td> containing the title and subtitles.
$rows = substr_count($iname, "\n") + 1;
for ($i = 0; $i < $rows; $i++) {
echo "<tr>";
if ($i == 0) { ?>
<td rowspan='<?php echo $rows;?>' width='25%'><?php echo $ptitle . "<br>" . nl2br($iname);?></td>
<?php }
?>
<td contenteditable="true" name="v1"></td><td contenteditable="true" name="v2"></td>...
</tr>
<?php }
DEMO
I'm kinda stuck here! I have to two tables free_members and domstic_members and I need to move selected rows from free_members tables to the domestic_members table. why I'm saying selected rows because of I at the end of every row in free_members tables there is an upgrade option which will move that row to the domestic_members table. I have the code but it's not working the way I want. right now what is happening is when I click on upgrade option it just copies the whole table records and sends it's to the other table. this here is the code
Controller
function upgradeprofile() {
// $this->load->database();
$this->load->model('freemember_model');
$this->freemember_model->upgrade();
}
model
function upgrade() {
$query = $this->db->get('free_members');
foreach ($query->result() as $row) {
$this->db->where('id', $member_id);
$this->db->insert('domestic_members', $row);
}
}
View
<center> Search Member:
<input type="text" name ='search' id='search' placeholder="Search a member"/></center>
<input type="hidden" name ='limit_from' id='limit_from'/>
</form>
<br><center><button type="button" onclick="CallPrint1('background12')">Print</button>
<div id="background12" class="box-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Sr#</th>
<th><center>Full Name</th>
<th><center>Mobile Number</th>
<th><center>Membership# / CNIC</th>
<th><center>Email Address</th>
<th><center>Province</th>
<th><center>District</th>
<th><center>Tehsil</th>
<th><center>Union Council</th>
<?php /*?> <?php if($sessiondata['deletemembers']):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<th><center>Delete</th>
<?php endif;?>
<?php /*?><?php if($sessiondata['data_modification'] && $sessiondata['username'] != 'bilal.sabir' ):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<th><center>Print</th>
<th><center>Edit</th>
<th><center>Change Pwd</th>
<th><center>Upgrade</th>
<?php endif;?>
</tr>
</thead>
<tbody>
<?php $sr=0;?>
<?php foreach ($freemembers as $member): ?>
<tr><td><center><?php echo $sr+=1; ?></td><td><center><?php echo $member->first_name; ?></td>
<td><center><?php echo $member->mobile; ?></td><td><center><?php echo $member->cnic; ?></td>
<td><center><?php echo $member->email; ?></td>
<td><center><?php echo $member->province; ?></td><td><center><?php echo $member->district; ?></td><td><center><?php echo $member->tehsil; ?></td><td><center><?php echo $member->uc; ?></td>
<?php /*?> <?php if($sessiondata['deletemembers']):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<td><center>delete</td>
<?php endif;?>
<?php /*?><?php if($sessiondata['data_modification'] && $sessiondata['username'] != 'bilal.sabir' ):?><?php */?>
<?php if($sessiondata['username'] != 'bilal.sabir'):?>
<td><center>print</td>
<td><center>edit</td>
<td><center>change pwd</td>
<td><center>Upgrade</td>
<?php endif;?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
this is the view of my table free_members
Based upon your View, you have this...
index.php/admin/upgradeprofile/" . $member->id
So if $member->id = 100 the url will be
index.php/admin/upgradeprofile/100
So you are passing a member_id as mentioned in your model code BUT you are not getting it nor are you using the where correctly.
So you need to get a handle on the member_id...
This is ONE Solution/idea...This relies on you providing a correctly validated integer... You need to always Check/validate your inputs...
Controller
function upgradeprofile($member_id) {
// Need to check that $member_id is an integer
// To be added by the OP
$this->load->model('freemember_model');
// $member_id is passed in via the URL.
$this->freemember_model->upgrade($member_id);
}
Model
function upgrade() {
// Which member do we want to get from the free_members Table?
$this->db->where('id', $member_id);
$query = $this->db->get('free_members');
// Did we get a result? i.e was a valid member id passed in?
if($query !== false) {
$row = $query->row();
$this->db->insert('domestic_members', $row);
} else {
// Do nothing or handle the case where an illegal number was used...
}
}
Note: This code is not tested and is only to be used as a guide.
It can be refactored a number of ways but I am trying to keep it simple for now.
So please, do not just copy and paste this without at least "thinking" about what you are trying to do and how the code might be written to achieve it.
The main points here are...
1. You have to use a member id of some kind so you can retrieve the record for the free member so you can insert it into the other table.
2. You are providing this "member id" in the url so you need to extract it.
3. You can do that by either setting it as a parameter in the method being called... Or you can use segments ( look it up ).
So once you have the member id you can fetch the free member entry and insert it into the other table BUT What do you want to happen to the entry in the free members table because that is still going to be there and now you have two copies of the same information... Delete it?
If you need further explanation - ask.
One last point.
You should be making sure you fully understand what is you are doing and not just doing it because someone said to or you thought it was a good idea... Have a Good Reason and think of the implications...
I'm not familiar with Codeigniter but MySQL syntax should be this:
INSERT INTO table1 (table1.row1, table1.row2)
SELECT table2.row1, table2.row2
FROM table2
WHERE PUT_YOUR_CONDITION_IF_NEEDED
I've found this for Codeigniter.
I'm building an exam management website and one of the pages I'm working on is for adding students to a course. I have a dropdown menu for the student number (which fetches values from a table), however I'd like to make it so that when the teacher selects the student number from the dropdown menu, that student's name and major appear on a table below. I have pretty much all the code for it however I can't seem to make it work. The way it is right now it shows the head of the table but it doesn't show any lines.
The errors are always in the lines where I declare $sql1 and $sql2 and vary according to how I define the condition in the statement.
Code for my dropdown menu : (works fine)
<label class="control-label" for="number">Student Number</label>
<?php
$sql = "SELECT number FROM students";
$result = $conn->query($sql);
echo "<select class=".'"form-control"'.' id="number" name="number" for="number">';
while ($row = $result->fetch_assoc()) {
echo '<option value="' . $row['number'] . '">' . $row['number'] . "</option>";
}
echo "</select>";
?>
Code for my table : (shows only head of table, which is the best I got after moving around the code and getting conversion errors and such)
The errors are always in the lines where I declare $sql1 and $sql2 and vary according to how I define the condition in the statement.
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Major</th>
</tr>
</thead>
<tbody>
<?php
$sql1 = "SELECT name FROM students WHERE number='$row'";
$result1 = $conn->query($sql1);
$value = $result1->fetch_object();
$sql2 = "SELECT major FROM students where number='$row'";
$result2 = $conn->query($sql2);
$value1 = $result2->fetch_object();
echo "<tr>
<td>".$value."</td>
<td>".$value1."</td>
</tr>";
?>
</tbody>
</table>
Thank you for all your help!!
Before I can formulate a complete answer, I must advise you that there are a few logical errors in your code.
How does your page "know" that a user selected an option from the select? You should perhaps intercept the event and respond to that using an asynchronoys mechanism, e.g. via AJAX.
Anyhow, there's no need to run two queries when you can make it with just one:
SELECT name, major FROM students WHERE number = ...
Once you have described how you mean to address issue #1 we can continue discussing the complete solution.
Well, I think there will be no $row in the the second snippet.
It seems that you didn't pass your $row from 1st snippet to 2nd snippet.
You can read this:
PHP Pass variable to next page
You can use session, cookie, get and post.
Or can just simply use "include", then the variables you defined can be used in the second page.
<?php
include "page1.php";
?>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Major</th>
</tr>
</thead>
<tbody>
<?php
$number = $row['number'];
$sql1 = "SELECT name, major FROM students WHERE number='$number'";
$result1 = $conn->query($sql1);
$value = $result1->fetch_object();
echo "<tr>
<td>".$value['name']."</td>
<td>".$value['major']."</td>
</tr>";
?>
</tbody>
</table>
According to godzillante's answer below, the mysql query should be like this:
Anyhow, there's no need to run two queries when you can make it with just one:
SELECT name, major FROM students WHERE number = ...
I notice that you use $row as the key of your second query.
But in the first snippet, the data you fetch is "$row" (it is an array, see PHP - fetch_assoc)
You should use $row['number'] instead.
I am at the end of my project, I have all the data from my database and everything is working fine I just can not work out how to display it in a table.
I have generated a table which is the (number of projects) * (the number of people).
The data I have collected is user_id, project_id and hours.
But how do I insert '6' (hours) into user_x's row in the column of the correct project?
I can only think to make x arrays (for the number of projects) of the length for the number of users and evaluate the project code to select the correct array and then use the user id to get the correct position to place the value and simply spit out the array into the td tag
This is incredibly messy, I wonder if I'm going about it completely wrong.
If this is indeed the best way I need to recursively create arrays and write code which references variables that might not even exist. Sounds insane to me
//for the length of projects create arrays that are the length of users and fill with 0's
for ($v = 0; $v < $rows_x; $v++){
$name = "variable{$v}";
$$name = array_fill(0, $rows_u, '0');
EDIT: What I am trying to do is show the number of hours that are booked to projects between two dates. I have gotten all the data correctly but now I need the data to land into a table so you can easily see which user booked to what project.
In an excel world I could use the project number to select the Y axis and the user id to select the X axis. However I don't know the best way to do this in php.
Surely creating an array for each project the length of the users and filling with data if there is data is not the best way.
I don't know if this is what you're looking for, but I insert my values into a table in the process of retrieving them.
You can echo the values into a <td> as long as they are in your SQL SELECT statement.
<table>
<thead>
<tr>
<th>Values1</th>
<th>Values2</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT value1, value2
FROM tbl_Values";
if (!$res = $link->query($sql)) {
trigger_error('Error in query ' . $link->error);
} else {
while ($row = $res->fetch_assoc()) {
?>
<tr>
<td>
<?php echo $row['value1']; ?>
</td>
<td>
<?php echo $row['value2']; ?>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
This is the way I put data into my tables, without using arrays.
I am trying to generate a table that takes data from a mysql database. There are 8 columns but the number of rows is variable depending on the amount of info from the database. The problem I'm running into is that I have two things that are variable and I don't know how to use two while loops (or if that's the right choice).
The code below can generate a table with 8 columns and a variable amount of rows but I don't know how to replace thing with sequential integers. I would like each cell to have just one integer in it sequentially like a while loop until the numbers = $end.
CODE:
<?php
$end=82; //will get all this data from a dabase
$rows=ceil($end/8);
$x=0;
$start=0;
?>
<table>
<?php
While ($x<=$rows){
echo"
<tr>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
</tr>
";
$x++;
}
?>
</table>
This will output eight numbers per row starting at 1 and going to $end. I am unsure of what $maxprobs is here.
<?php
$end=82; //will get all this data from a dabase
$rows=ceil($end/8);
$x=0;
$start=0;
?>
<table>
<?php
while($x<=$rows) {
echo "<tr>";
for ($y = 0; $y < 8; $y++, $start++) {
if ($start <= $end) echo "<td>$start</td>";
}
echo "</tr>";
$x++;
}
?>
</table>
I have to ask - if you're pulling rows from a MySQL database, and you want your table to have a dynamic number of rows depending on the data, is there any specific reason you're not doing it the regular way, with a while loop and a mysql_fetch function?
$data = mysql_query("SELECT row1, row2, ... , row8 FROM table WHERE ...); // Sample query, edited for brevity.
echo "<table>";
while ($result = mysql_fetch_object($data))
(
echo "<tr>";
echo "<td>".$data->row1."</td>;
echo "<td>".$data->row2."</td>;
... // Edited for brevity. Include as many columns as you queried.
echo "<td>".$data->row8."</td>;
echo "</tr>";
}
echo "<table>";
Unless you have a really specific reason not to do it this way, I'd use this method. It's flexible and it will print as many rows as your query returns (meaning that you only need to change your query, not your code).
Additional information on mysql_fetch_object and mysql_query can be found here and here, respectively.
Take a look at the php website, nearly all their entries have examples, the database stuff shows a while loop, this is the best way to do it..
write the table opening tag, plus any headers..
while able to get more database data, get it, and do
display the table row open tag
display the row, by looping through the fields, and outputting them between cell tags
dispaly the end of row tag
now you've finished with your data, write the end of the table
There are a lot of examples about.
for the looping through the field, thats why they invented a "foreach" , eg wether you have 0, or 10000, you can go through each one, even if its not consecutive, so for each "fruit" in apple, pear, bannana..