I am a newbie in php and working on something something and I can't figure out how will it work. I have a query like this which will display specific rows from the database:
<?php
include ('test/tru/conek.php');
$result=mysql_query("SELECT * FROM post_jobs WHERE job_category='Administration'");
while($row=mysql_fetch_array($result)){
?>
<tr >
<th style="font-weight:normal"><input type="text" name="name" value="<?php $name=$row['company_name']; echo $name ; ?>" readonly="readonly" /></th>
<th style="font-weight:normal"><?php echo $row['job_responsibilities']; ?></th>
<th style="font-weight:normal" id="industry"><?php echo $row['industry']; ?></th>
<th style="font-weight:normal" id="date"><?php echo $row['date']; ?></th>
<th style="font-weight:normal" id="loc"><?php echo $row['location']; ?></th>
</tr>
</table>
<?php } ?>
My question is, how could I display or determine the information on a specific row from clicking the link and then displaying it?
It seems like you want differentiate between the rows after printing them onto screen. The most common way of doing this is by using an id number for each row. You add an 'id' row which is auto incrementing and primary into your mysql table. You would then add it to the link in your html:
<th style="font-weight:normal">
<a href="#" onclick="ss(<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>)" name="res">
<?php echo htmlspecialchars($row['job_responsibilities'], ENT_QUOTES, 'UTF-8'); ?>
</a>
</th>
You would then do something with that id number in Javascript in your ss() function. If you infact don't want to use Javascript at all, then you should write your link tag as:
<th style="font-weight:normal">
<a href="somewhere.php?id=<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>" name="res">
<?php echo htmlspecialchars($row['job_responsibilities'], ENT_QUOTES, 'UTF-8'); ?>
</a>
</th>
I think that's what you're looking for.
Edit: How silly of me, I also forgot to mention that you should never print user-supplied data in your html without first escaping it. This is to prevent Cross Site Scripting (XSS) which is a common yet very serious vulnerability in web pages. I've edited my code accordingly. You should replace the 'UTF-8' with the correct encoding of your data. This is the reason you should also explicitly state the encoding: http://shiflett.org/blog/2007/may/character-encoding-and-xss
Related
I have been trying to use various combinations of anchor and html tags to code my third row as an actual URL (each time) instead of plain text, and it's not working. To be clear, I am not trying to hardcode a single URL and output as a hyperlink but rather attempting to code in such a way that this third row is consistently outputted as a clickable hyperlink. This is a .php page, and the links are .pdf URLs. I'm also finding this a little more difficult to do because it's a table (so, extra formatting there).
What I'm doing is retrieving many articles from my database, each with the third row as a URL, and then using the below code to output it in my webserver, using tags, with the php . as a separator, etc. However, I'm not able to output the row, which currently displays as plain text, as a set of clickable hyperlinks.
I'm not looking for how to HTML link or output a single link as a hardcoded hyperlink. That's not what I'm asking. I'd also like someone to take the third line below and provide a demo.
<table class="table" width="920" style="border:thin" cellpadding="10" border="1">
<tr>
<th>Topic</th>
<th>Category</th>
<th>URL</th>
</tr>
<?php
while($row = $result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['Topic']; ?></td>
<td><?php echo $row['Category']; ?></td>
<td><?php echo $row['URL']; ?></td>
</tr>
<?php
}
?>
</table>
Your third line should be like this
<td> the link text </td>
if you need the link and text to be same the code will look like this
<td><?php echo $row['URL']; ?></td>
Basically, its like this
<td> ECHO THE URL TEXT HERE </td>
In HTML you can create a link with an anchor tag <a>.
Please try this:
<td>
<a target="_blank" href="<?php echo $row['URL']; ?>"> <?php echo $row['URL']; ?></a>
</td>
From a database we are getting data, but my question is that after tbody starting tag and before tbody ending tag, why we are ending php in this way using <?php }?>
Why is it used in these lines?
<div class="container">
<div class="table-responsive">
<table class="table table-striped" >
<thead>
<tr>
<th scope="col">CNIC</th>
<th scope="col" >Name</th>
<th scope="col" >DOB</th>
<th scope="col" >Address</th>
<th scope="col" >City</th>
<th scope="col" >Degree Program</th>
<th scope="col" >Gender</th>
<th scope="col" >Email</th>
<th scope="col" >Mobile</th>
<th scope="col" ></th>
<th scope="col" ></th>
</tr>
</thead>
<tbody>
<?php while($student = mysqli_fetch_assoc($resultSet)){?>
<tr>
<td scope="row" ><?php echo $student['cnic']; ?></td>
<td><?php echo $student['fname'] . " ". $student['lname']; ?></td>
<td><?php echo $student['dateofbirth']; ?></td>
<td><?php echo $student['address']; ?></td>
<td><?php echo $student['city']; ?></td>
<td><?php echo $student['degree']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['email']; ?></td>
<td><?php echo $student['mobile']; ?></td>
<td> <a class="btn btn-primary" href=<?php echo "update_student.php?u_id=".$student['u_id']; ?> >Update</a> </td>
<td> <a class="btn btn-primary" href=<?php echo "delete_student.php?u_id=".$student['u_id']; ?> >Delete</a> </td>
</tr>
<?php }?>
</tbody>
</table>
</div>
Here,
like your code snippet, we can decide what is to be displayed in the DOM (html page).in your code the whole contents in
<tr>...</tr>
will print on the html page only till the while condition satisfies.if the while loop runs for ten times there will be ten rows in
<tbody>...</tbody>
here the while loop will continue till fetching the last record from the specified table.
This:
<?php }?>
Is actually closing your while loop that starts at the begining:
<?php while($student = mysqli_fetch_assoc($resultSet)){?>.
If that would not be present, the code would throw you an error.
Loops in PHP (should) start and end with curly brackets. So at the begining you have something like:
for($i; $i< 10, $i++) {
or
foreach($a as $b) {
or
while($someCondition){
And at the end always a closing curly bracket:
}
BR
With this <?php ..... ?> inside html code, you are (probably) showing html code inside php file. So basically you have (for example) a HTML template in php file, hence the php opening/closing tags. So while you are tehnically in a PHP script, you are actually showing something like:
<!DOCTYPE html>
<html>
<head>
<title><?php echo "Some title" ?></title>
</head>
<body>
<h1><?php echo "Hello World" ?></h1>
</body>
</html>
If you can, I'd suggest to use a templating engine for this, like twig.
So in your case, you are fetching result from DB, looping over it and shwoing it in HTML table.
BR
PHP has a cool way of deciding to show content. Instead of echo'ing HTML based on a variable, you can use the PHP <?php if (...): ?> html here <?php endif; ?> and the HTML inside will show only if that if condition is met. It's the same thing here, except with a while instead of an if
A PHP while loop works like this:
while($x === true){
//execute some code
}
This will execute the code inside the while loop an infinite number of times, or until the $x variable is changed (within the loop) to equal true. The opening and closing brackets, { and } are what determine where the while loop starts and ends. In your code, PHP is printing out all of those <tr> and <td> tags until mysqli_fetch_assoc($resultSet) no longer returns a result. The line <?php }?> just tells the program, "the loop stops here, go back to the start"
Hello and thanks for coming.
So I have the Category table with "n" values on it, and I have the table Price with "n" duplicated idCategorys (foreign Key) in it.
What I want to do is when selecting a Category from a dropdown menu a Html Table gets populated with all the prices with the Category i've chosen.
So far I've managed to get a dropdown select menu populated with php as follows:
<select name="categorias" required id="categorias" >
<?php $render = mysqli_query($con,"SELECT idCategoria FROM neomixlt_desarrollo.Categorias");
while($row1 = mysqli_fetch_array($render)) {
echo "<option value='" . $row1['idCategoria'] . "'>" . $row1['idCategoria'] ."</option>";
}
?>
</select>
And a table populated as follows
<table width="957" border="2" cellspacing="0">
<tr>
<th width="98" style="text-align: center">Categoria</th>
<th width="99" style="text-align: center">Articulo</th>
<th width="348" style="text-align: center">Precio a Domicilio(bs.)</th>
<th width="348" style="text-align: center">Precio en Planta(bs.)</th>
<th width="40" style="text-align: center"></th>
</tr>
<?php $render = mysqli_query($con,"SELECT * FROM neomixlt_desarrollo.precios_articulo WHERE idCategoria=$link ORDER BY idCategoria DESC ") or die(mysql_error()); ?>
<?php while($row1 = mysqli_fetch_array($render)):; ?>
<tr>
<td style="text-align: center"><?php echo $row1['idCategoria']; ?></td
><td style="text-align: center"><?php echo $row1['idArticulo']; ?></td>
<td style="text-align: center"><?php echo $row1['precio_domi']; ?></td>
<td style="text-align: center"><?php echo $row1['precio_plant']; ?></td>
<td style="text-align: center"><?php echo "<a href=editar_precios.php?pid=$row1[idArticulo]&cid=$row1[idCategoria]&precio_domi=$row1[precio_domi]&precio_plant=$row1[precio_plant]>editar</a>" ?></td>
</tr>
<?php endwhile; ?>
</table>
But they are not linked and I have not much idea on how to do this. im restricted to use php and html only and all similar examples I've found are using javascript or ajax, any idea? thanks in advance!
w/o JavaScript, JQuery, Angular, etc. available, you'll need to submit every time.
So load the page up in its original state, the user will choose their category. Now w/o JS available to go run updates nicely for you, you'll need to submit a form back to the server, then on the server side, get the POST category, and build the desired HTML and send that to the browser to view. This will have to be done every time they change categories.
Im trying to echo a variable into an already existing DIV within a table.
EG:
<table>
<tr>
<td class="error"> </td>
</tr>
</table>
<?php
echo "<td class='error'>".$variable."</td>";
?>
Note: the error td tag is already style and positioned within a table.
The method above causes a lone td tag to be created and added to the top of
the page.
I realise it's probably best to echo php in HTML rather than vice versa but
I need echo HTML in this instance.
Is this possible ?
The simplest solution, if I am understanding correctly:
<table>
<tr>
<td class="error"><?php echo $variable; ?></td>
</tr>
</table>
You have to echo in-place.
EDIT: Solved - was not flutter's tag stripping, should work as advertised.
I'm using Flutter (which creates custom fields) in Wordpress to display profile information entered as a Post. Before I implemented the CSS tables the link showed up and was clickable. Now I get nothing returned, even when I try to call the link outside the table.
If you know anything about this, here's my code in the index.php file and I remain available for any questions.
<?php if (in_category('Profile')) { ?>
<table id="mytable" cellspacing="0">
-snip-
<tr>
<th class="row1" valign="top">Website </td>
<td>Link: <?php echo get_post_meta($post->ID, 'FrWebsite', $single=true) ?></td>
</tr>
-snip-
</table>
Edit: #Josh - there is a foreach looping construct in the table and it is reading and displaying the code correctly, I see what you're getting at now:
<tr>
<th class="row2" valign="top">Specialities </td>
<td class="alt" valign="top"><?php $my_array = get('Expertise');
$output = "";
foreach($my_array as $check)
{
$output .= "<span>$check</span><br/> ";
}
echo $output; ?></td>
</tr>
Edit - #Josh - here's the old code as far as I can remember it, there was no major difference just a <td> tag where there now stands a <th>, there wasn't the class="" and there was no "Link:" and FrWebsite was called Website, but it still didn't work when called Website so I changed to see if that was the error.
<tr>
<td width="200" valign="top">Website </td>
<td><?php echo get_post_meta($post->ID, 'Website', $single=true) ?></td>
</tr>
Where is $post set? What does the full table look like? Could be that when you changed the table structure you accidentally removed something like (line 2 below):
<table id="mytable" cellspacing="0">
<?php foreach($posts as $post) { ?>
<tr>
<th class="row1" valign="top">Website </td>
<td>Link: <?php echo get_post_meta($post->ID, 'FrWebsite', $single=true) ?></td>
</tr>
<?php } ?>
(Just guessing here...)