I have a mysql table I want to echo the name of every user and on click on a certain user show his profile:
$user=mysql_query("SELECT * FROM signup ORDER BY name ASC ");
while($row = mysql_fetch_array($user)){
echo ''.$row['nume'].' '.$row['prenume']."";
echo "<br>";}
The problem is that i can not figure out how to concatenate $row['username'] with href
The output should look like this: www.abcde.de/profil?user=username
Try this
echo ''.$row['nume'].' '.$row['prenume'].'';
Related
This is my code and it is inside table tags. It grabs data from my database and put it in table. My database also has auto increment column named id. What I want to do is when I click the cross icon that is in anchor, I will get the id for that column.
I tried putting the value of $row['id'] in an input type='hidden' but had no success with it.
Another idea I has is making an anchor id=".$row['id']." so every icon has a unique id pertaining to the id for that line. Though I don't if it is good. I can't check because I have no idea how to get an element's id so I can check. And I don't know how to pass this value to php.
<?php
$varSQL = "SELECT empNum, empName FROM tool.users";
$result = mysql_query($varSQL, $Con);
while ($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['empNum']."</td>";
echo "<td>".$row['empName']."</td>";
echo "<a href='#'><img src='icons/cross.ico' alt='Delete' height='16' width='16'></a></td>";
echo "</tr>";
}
?>
You can use data attribute -
echo "<a href='#'><img src='icons/cross.ico' alt='Delete' data-rowid='" . $row['id'] . "' class='cross-icon' height='16' width='16'></a></td>";
Also added a class to be be used as selector(Not required for all cases). And with jquery access them when the icon is click -
$('.cross-icon').on('click', function() {
var id = $(this).data('rowid');
})
Fiddle Example
For hidden fields there is no need of jQuery -
<input type="hidden" name="id" value="<?php echo $row['id']?>"
Or pass it as query string -
href='page-to-redirect.php?id=<?php echo $row["id"]?>'
What you want to do with this id? You can get the id using javascript (not PHP) and then using this value in an AJAX request or put this value into an input form and send it throw post but always you need to use JS not PHP.
It is because you do not have id in your query. It should look like this:
SELECT id, empNum, empName FROM tool.users
<?php
$varSQL = "SELECT id, empNum, empName FROM tool.users";
.........
echo "<td><a href='#'><img src='icons/cross.ico' data-rowid=$row['id'] alt='Delete' height='16' width='16'></a></td>";
........
Try this one.
In select query add id also and Get the data-rowid value for your further operations
I have made a connection to mysql database and echoing values from a table.
while($data = mysql_fetch_array($res))
{
?>
<a href="nextpage.php"<?php echo $data['rowname'] ?></a>
<?php
}
?>
Problem is when I click on the particular link, on the nextpage.php, it should display only the result of the value of a href clicked. So on the nextpage.php, I have defined something like SELECT * from tablename where rowname = 'a href value'.
What's happening now is that it displays only the last rowname value regardless of whichever link I click on, very obvious!
I have tried forms, arrays, SESSIONS but to no avail. How do I fix this?
the href should be like this
<?php echo $data['rowname']; ?>
and then on next page you can use $_GET['val'] and pass it to SELECT query
Try example as below
page1.php
while($data = mysql_fetch_array($res))
{
echo "<a href='nextpage.php?id=".$data['rowname']." >". $data['rowname'] ."</a>";
}
?>
your href will look like nextpage.php?id=[your value]
nextpage.php
$qry = mysql_query("select * from [table_name] where id = ".$_GET['id']."",$con);
while($data = mysql_fetch_array($res))
{
echo $data[0];
}
on nextpage pass value using $_GET['id'] to sql query.
i have a small website where people can submit name ideas for a project i am working on
Two things i want to implement are an upvote button (+1 to current score)
and when someone submits a name it will update the mysql array using ajax.
I am very new to Ajax and i have managed to work some code together that submits a name without leaving the current page.
This is my code for the mysql array that displays underneath the name submission form.
How can i link this upto a form submit button for refresh when a name is submitted?
<?php
$result = mysql_query("SELECT * FROM namevotes ORDER BY upvote desc")
or die(mysql_error());
echo "<center>";
echo "<table border='1' id='tablea'>";
while($row = mysql_fetch_array( $result )) {
echo "<tr><td>";
echo $row['name'];
echo "<tr><td>";
echo $row['upvote'];
echo "</td></tr>";
}
echo "</table>";
echo "</center>";
?>
I am want to add a button next to each row that will increase the "upvote" by one, i know how to rig one up that will manually upvote a single entry, im not sure how to direct it to each array entry.
Method is very simple:->
1> add button in while clause using <button id="{row['id']}">like me</button>
2> create a jquery element in webpage that onclick on hitlike button trigger a event: fetch data from url by get {or post} method with data ?hitlike=id
3> before code in php tag, put if statement that if(isset($_GET['id'])) then UPDATE table SET likes=likes+1 WHERE id=$_GET['id']
4> rest code will be the same to show data
//I haven't tried this but I think it should work
I'm retrieving products from my database and listing them in a while loop. Like this
<?php
while($row = mysql_fetch_row($result)){
echo '<div class="product_info">';
echo '<div class="category_product_title">Product';
echo '</div>';
echo '<div class="category_product_number">#'.$row[0].'('.$row[1].')'.'</div>';//prints id and name
echo '<div class="category_product_description">'.$row[2].'</div>';//prints description
echo '<div class="category_product_price">'.$row[4].'TL</div>';//prints price
echo '<div class="category_details">DETAILS</div>';//The link to list the product details
echo '</div>';
echo '</div>';
}
?>
I want to be able to print more details on a paticular prodcut when the "DETAILS" link is clicked by getting the id or name of that paticular product and using to query the database.
I tried saving the id in a session variable like this $_SESSION['id'] = $row[0] but since i'm looping through these products, i'm not able to get the id of a specific product when it is clicked. How can i do this?. I'm confused because of the loop. Thanks
You can assign a different link to each one of the products with a GET variable.
Instead of DETAILS, the link could point to DETAILS.
Then, on the product page productpage2.php, you can get the selected ID from the $_GET superglobal array like this:
$id = $_GET['id'];
Lots of ways to do this. I would probably do something like:
echo '<div class="product_info" data-product-number="' . $row[0] . '">';
Then just grab the data-product-number attribute with JavaScript and pass it along with the request. Obviously, you'll need to verify this on the server side somehow once the request is made, since a user could set the product number in the request to anything they'd like.
I am new to php, mysql. currently i'm building a website. with the search query the data is displayed well. (in this case the search query is 'name %like%'). now i want to open another page that will display the profile of any one of the name displayed (when clicked) in the previous query. how to acheive this?
you'll have to select, for example, user_id when searching.
and then, your code could look like
<?php
include("db.php");
$result = mysql_query("SELECT user_id, username FROM user WHERE username LIKE '%".mysql_real_escape_string($_POST['username'])."%'");
echo "<table>";
while($row = mysql_fetch_assoc($result)
{
echo '<tr><td>'.$row['username'].'</td></tr>';
}
echo "</Table>";