I have a table that is displaying data to the user and each row in the table has a unique id, in the table I am generating a button for each row to get more information on the content in the row. However, i need to generate a unique href for each button based on the Unique ID of the data in the row. The way I have done it it correctly goes to the link in the href but without passing the Unique ID.
<tbody>
<?php while($row = $query->fetch()) { ?>
<tr>
<td><?php echo $row['car_id']; ?></td>
<td><?php echo $row['car_make']; ?></td>
<td><?php echo $row['car_model']; ?></td>
<td><?php echo $row['number_available']; ?></td>
<td><?php echo $row['rent_cost_per_day']; ?></td>
<td><a class="btn btn-default" href="cars.php?"<?php $row['car_id']; ?>>View</a></td>
</tr>
<?php } ?>
</tbody>
Any help?
This is because you put the php after you closed the href quotes. You also have not put an echo function for the php, and have not put a get variable. You have:
href="cars.php?"<?php $row['car_id']; ?>
What you want to have is:
href="cars.php?id=<? echo $row['car_id']; ?>"
Related
Okay, my problem is that how can I set different hyperlinks to each row of a table in php?
so far i've done this -->
<?php
$conn = mysqli_connect("localhost", "root", "", "makaut_colleges");
$result = mysqli_query($conn, "SELECT * FROM collegelist");
while ($row = mysqli_fetch_assoc($result)):
?>
<tr>
<td><?php echo $row['College Name']; ?></td>
<td><?php echo $row['College Address']; ?></td>
<td><?php echo $row['College Code']; ?></td>
<td><?php echo $row['Review']; ?></td>
</tr>
<?php endwhile; ?>
How can i add different hyperlinks to each row which will be extracted from database? i'm using 'College Code' as primary key.
You can try something like this
Try this for making tr clickable
$dynamic_link = "dynamic.php";
<tr onclick="document.location = '<?php echo $dynamic_link ?>'">
td code---------
</tr>;
where $dynamic_link is dynamic based on your requirement
Thier should be some logic or linking with url either save in table or create some link
fetching some data from mysqli
$value=mysqli_fetch_assoc($query)
I have a result like
$value = "123456789101112131415161718192021222324252627282930";
but I want to use it two variables like
$value1 = "123456789101112131415";
$value2 = "161718192021222324252627282930";
Use the database column names.
If you don't have the value you need in separate database column you have to somehow extract the value from an existing column.
You can use substr or regex to do so.
<?php while($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['DATABASE_COLUMN_1_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_2_NAME']; ?></td>
<td><?php echo $row['DATABASE_COLUMN_3_NAME']; ?></td>
<!-- show only a part of the db columns content -->
<?php $valueIwantToShow = subtr($row['DATABASE_COLUMN_4_NAME'], 0, 5); ?>
<td><?php echo valueIwantToShow; ?></td>
<!-- or show multiple values in one html column -->
<td><?php echo $row['DATABASE_COLUMN_6_NAME']; ?>, <?php echo $row['DATABASE_COLUMN_7_NAME']; ?></td>
</tr>
<?php } ?>
i have created a php page for fetch all data from mysql and i have another page to show data on webpage....
i have php page its like page_1.php
<?php include_once'db_localconnection.php';
$query="SELECT * FROM `table 5` where base='Home Plans'";
$get_allplans=mysql_query($query) or die(mysql_error());
while($fetch=mysql_fetch_array($get_allplans))
{
$plans_code=$fetch['plan_code'];
$speed=$fetch['speed'];
$data=$fetch['data'];
$duration=$fetch['duration'];
$gb_pm=$fetch['gb_pm'];
$up_speed=$fetch['up_speed'];
$price=$fetch['price'];
$base=$fetch['base'];
}
?>
and i have another page for show data i have also include all needed pages
<td><?php echo $plans_code; ?></td>
<td><?php echo $speed; ?></td>
<td><?php echo $data; ?></td>
<td class="center"><?php echo $duration; ?></td>
<td class="center"><?php echo $gb_pm; ?></td>
<td><?php echo $up_speed; ?></td>
<td><?php echo $price; ?></td>
<td>get reacharge</td>
so i am getting only first record please help..
You storing the values from db to variables i.e $plans_code, etc. in WHILE statement you just overwriting the values each time you loop. Instead, store them into array and send to your second page and display them.
example:
$completeData = array();
while($row=mysql_fetch_array($get_allplans))
{
array_push($completeData, $row);
}
now fetch the array $completeData to your second page and then display it,
like:
<?php foreach($completeData as $row) { ?>
<tr>
<td><?php echo $row['plans_code']; ?></td>
<td><?php echo $row['speed']; ?></td>
.
.
.
</tr>
<?php } ?>
This is actually very easy but yet I am confused how to do this:
I have Reseller that can request X-number of users to admin. So that value gets saved to database. Now when admin logs in he can see the requests of resellers. Like this :
Now Admin Can either approve or reject the request. If He approves the request the code creates users in users table mapped to that reseller. The mapping is done with a unique field called Key Now Creating users works perfectly but my problem is :
When I click on Approve it creates users but for only for the reseller wit id = 1. I want to create something like when I click on first record it creates users for 1st reseller and when on 2nd same as above[This is as per the image].
This is my view:
<?php if(count($users)): foreach($users as $user): ?>
<tr class="odd gradeX">
<td><?php echo $user->id; ?></td>
<td><?php echo $user->user_requested; ?></td>
<td><?php echo $user->key; ?></td>
<td><?php echo $user->date_requested; ?></td>
<td>Yes <i class="glyphicon glyphicon-ok"></i></td>
<td>No <?php echo'<i class="glyphicon glyphicon-trash"></i>';?></td>
<td><?php echo $user->status; ?></td> </td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">There are no new Requests.</td>
</tr>
<?php endif; ?>
The Controller
public function create_user()
{
$this->load->model('more_m');
$id= $this->session->userdata('id');
$rajan =$this->more_m->get(array('user_requested',$id));
$request=$rajan->user_requested;
$this->load->model('reseller_m');
$query=$this->db->select('key');
$query=$this->db->get('reseller');
if($query->num_rows() > 0)
{
$row = $query->row_array();
$key= $row['key'];
}
for($i=1; $i<=$request;$i++)
{
$userdata=array('key'=>$key);
$this->db->insert('users',$userdata);
}
$this->load->model('more_m');
$id = $this->session->userdata('id');
$this->db->set('status', "'approved'",FALSE);
$this->db->where('id',$id);
$this->db->update('user_request');
redirect('admin/new_user');
}
My mistake is in controller where I define $id. As when admin logs in his id would be fetched I don't know how to fetch the id of the reseller as per the record.
Its so simple with Codeigniter
What you have to do is pass the ID with the URL (You are wrong Use base_url()as well with the pointing controller)
ex <td><a href="<?php echo base_url()?>reseller/create_user/<?php echo $user->key; ?>"
so now your URL looks like (assume id is equal to 2)
http://localhost/<path to project>/reseller/create_user/2
so in Controller
function create_user($id)//So this know incoming Value to this
{
//then in here
echo $id; //this will show 2
//Continue rest of your code with $id
}
If you wanna do it in php way,You can pass key in your url and get key in your controller using $this->uri->segment(n)
<?php if(count($users)): foreach($users as $user): ?>
<tr class="odd gradeX">
<td><?php echo $user->id; ?></td>
<td><?php echo $user->user_requested; ?></td>
<td><?php echo $user->key; ?></td>
<td><?php echo $user->date_requested; ?></td>
<td>Yes <i class="glyphicon glyphicon-ok"></i></td>
<td>No <?php echo'<i class="glyphicon glyphicon-trash"></i>';?></td>
<td><?php echo $user->status; ?></td> </td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">There are no new Requests.</td>
</tr>
<?php endif; ?>
If you want jquery+php than use ajax.
add value attr. to button pass your key.
<button id="some_id" value="YOURKEY">Click Me</button
This is your script to update :
<script>
$('body').on('click',"#some_id", function(){
var key =$(this).attr("value");
//Now you got ur key you can use ajax to update and change location of page on success
});
</script>
I have the following table:
foreach ($forwards as $data):
?>
<tr style="cursor: pointer" class="main_row">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php
endforeach; ?>
now the idea is that when ever you click one of the main_row it should redirect to a new url. the problem is that the url contains an id for instance the url could look like www.example.com/details/2 where 2 is the id.
This id is like all of the other data stored in a php variable: $data['Offer']['id'];
Now my question how can i redirect using both php and javascript? is there a work around? .
Please do note that i am fully aware that php i server side and javascript is not. it is because of this i am asking this question.
<table>
<?php foreach ($forwards as $data): ?>
<tr data-link="http://www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<td><?php echo $data['Offer']['id']; ?> </td>
<td><?php echo $data['Offer']['name'];?> </td>
<td><?php echo round($data['Stat']['ltr'],2)."%"; ?></td>
<td><?php echo round($data['Stat']['cpc'],2)."%"; ?></td>
<td><?php echo $data['Category']['name']?></td>
<td><?php echo $data['Country']['name'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<script>
$('table tr').click(function(){
document.location.href = $(this).data('link');
});
</script>
if you use jQuery though.
You can get the ID from the first column in the row:
$(".main_row").click(function() {
var id = $(this).find("td:first-child").text();
window.location.href = 'www.example.com/details/' + id;
});
If you use jQuery do this:
$('.main_row').click(function(){
window.location.href = "YOUR_URL"+$(this).attr('id');
});
And modify the html for the row to look like:
<tr style="cursor: pointer" class="main_row" id="<?php echo $id ?>">
This way - any time you click on the row it appends the row ID to the url and redirect you there.
If I am understanding you question correctly, your not looking for a redirect. To redirect in php, it must be done before you echo anything to the browser.
Because you want to "redirect" after your display is rendered, you should be using anchors.
<td><a href="www.example.com/details/<?php echo $data['Offer']['id']; ?>">
<?php echo $data['Offer']['id']; ?>
</a></td>
The anchor will direct the browser to the href url.