I have the code below:
$result = mysql_query("SELECT link, notes FROM links WHERE username='will';");
$html .= "<ul>";
while ($row = mysql_fetch_array($result)) { //loop
extract($row);
$html .= "<li>{$link} - {$notes}</li>";
}
I need the bit where it says {$link} to become a clickable link which opens a new window. How would I do this?
When I put tags around it you get this error:
The error is: Parse error: syntax error, unexpected '{' in /data/www/vhosts/themacsplash.com/httpdocs/ClipBoy/will.php on line 18
Line 18 is $html .= "<li>{$link} - {$notes}</li>";
In general you make a link like this: link title. So in your case like this:
$html .= "<li>{$link} - {$notes}</li>";
First create a correct code and make error handeling, than set variables outside quotes.
$qry = "SELECT link, notes FROM links WHERE username='will'";
$mysqlqry = mysql_query($qry);
if($mysqlqry){
if(mysql_num_rows($mysqlqry) > 0){
$html .= "<ul>";
while($row = mysql_fetch_array($result)) { //loop
extract($row);
$html .= "<li>". $notes ."</li>";
}
}
}
if your $link contains an url in the form "http://www.example.com/", use this:
$html .= "<li>{$notes}</li>";
Related
I am attempting to add a date function I created to my JSON data that I am sending back with PHP. I cannot figure out how to properly assign the function in my separate php file and send back. The function is on my main page (comments page) and not in this php file.
However, on my main comments page, when not using JSON (just a normal PHP SELECT query, the function works just fine.
I do it on my comments page (the page without the issue) like this:
$comment_array[] = $comment_date;
echo '<div class="comment-post-date">'.fixDate($comment_date). '</div>';
This is how I try to do it in the php file that isn't working when sent back. It is actually giving an error in the console:
Unexpected token <
I attempt to assign it as a variable and then send it back as this:
$html .= '<div class="comment-post-date">'.$fixed_comment_date. '</div>';
Full code:
if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
$select_comments_stmt->execute();
$rows = $select_comments_stmt->fetchAll(PDO::FETCH_ASSOC);
$comments = array();
foreach ($rows as $row) {
$comment_date = $row['date'];
$fixed_comment_date = fixDate($comment_date);
$html = "";
$html .= '<div class="comment-post-box" id="comment-'.$row['id'].'">';
//$html .= '<img class="home-comment-profile-pic" src="'.$row['img'].'">';
$html .= sprintf(
'<img class="home-comment-profile-pic" src="%s">',
empty($row['img']) ? 'profile_images/default.jpg' : $row['img']
);
$html .= '<div class="comment-post-info-block">';
$html .= '<div class="comment-post-username">'.$row['username']. '</div>';
$html .= '<div class="comment-post-date">'.$fixed_comment_date. '</div>';
$html .= '</div>';
$html .= '<div class="comment-post-text">'.$row['comment']. '</div>';
$html .= '</div>';
$data = array('id' => $row['id'], 'date' => $row['date'], 'html' => $html);
$comments[] = $data;
}
}
echo json_encode($comments);
Any ideas how I can get this to work?
Unexpected token < usually means your script didn't output JUST json. It output html+json, which is illegal JSON.
If you're doing a request for which JSON is expected as the response, then any OTHER output, other than the actual json response, will be treated as a JSON syntax error.
e.g.
echo '<div>blahblalblah</div>';
echo json_encode($whatever):
will cause your unexpected token error, because the first < in the <div> line is already a JSON syntax error.
That also means that things like PHP warnings/errors that are output will also become part of the response, and ALSO trigger a syntax error.
You shouldn't take by json HTML code only mysql results, e.g.
$comments['id'][] = $row['id'];
$comments['username'][] = $row['username'];
//next one
//next one
//next one
echo json_encode($comments);
And in JS code:
var count = json.id.length;
for(var i = 0; i<count; i++)
{
var id = json.id[i];
var username = json.username[i];
}
I am trying to populate a drop-down menu with values from an array.
I have tried to follow other answers but the syntax doesn't seem to be working. (I'm still relatively new to PHP).
The following code which I am working on was produced by someone else.
$sqlite_query = "SELECT * FROM dis_kind";
$result = $db->query($sqlite_query);
$array = $result->fetchArray();
$output = "<select name=\"kind\" class=\"dis\" >\n";
$output .= "<option value=\"$this->wildcard_value\"></option>\n";
foreach ($result as $array) {
$value = $array['kind'];
$output .= "<option value=\"";
$output .= $value;
$output .= "\">";
$output .= $value;
$output .= " - ";
$output .= $array['description'];
$output .= "</option>\n";
}
$output .= "</select>\n";
I don't know why it has been done the way it has but I am stumped as to getting my drop-down to work.
Currently, the box appears but is populated with no values.
Thanks.
Eventually managed to solve the problem myself - before I pulled my hair out!
Instead of using the foreach loop, I used the following:
while($array = $result->fetchArray())
{
// Output code.
}
Which populated the drop-down with values from the array.
I'm generating dynamic content from a database like this:
$sql_select_items = $db->query("SELECT * FROM table WHERE ...);
Then it pulls the results, which may be one or multiple, like this:
while ($item_details = $db->fetch_array($sql_select_items))
{
$items_content =
'<table><tr> '.
'<td>RETRIEVED CONTENT HERE</td> '.
'</tr></table>';
}
Then, further down, I am outputting the generated content like this:
if ($section == 'summary_main')
{
$summary_page_content['content'] =
$summary_page_content['details'] .
$items_content .
$summary_page_content['messaging_received'] .
$summary_page_content['footer']
;
$template->set('members_area_page_content', $summary_page_content['content']);
}
Everything works except for the content generated by $items_content , which only displays 1 item no matter if there are 1 or 20. I tried to do a
$items_content . =
instead of
$items_content =
but that didn't seem to work either and just gave me an error.
What am I doing wrong?
It's not $db->fetch_array($sql_select_items) it's $sql_selected_items->fetch_array(). Here's how I would do it:
$table_rows = $db->query("SELECT * FROM table WHERE ...");
if($table_rows->num_rows > 0){
$table = '<table><tbody>';
while($row = $table_rows->fetch_object()){
$table .= "<tr><th>{$row->title_column_name}</th><td>{$row->other_column_name}</td></tr>";
}
$table .= '</tbody></table>';
}
else{
$table = '';
// no results
}
echo $table;
$table_rows->free(); $db->close();
Outside of (before) the while loop:
$items_content = '';
Inside the while loop:
$items_content .= '...';
This will make sure that your $items_content variable exists before your loop, and then the .= will concatenate your string to the end of $items_content.
"What am I doing wrong?"
As you didn't post the error, I can only assume you either got an undefined variable notice, which is solved by placing $items_content = ''; before the loop, or your got a syntax error because the operator you should be using is .= and not . = (note that the space is wrong).
I am having a hard time on creating a multiple checkbox ajax delete. I am an ajax noob.
$sql = 'SELECT * FROM users';
$qry = mysql_query($sql);
$html = '';
$html .='<center>';
$html .= '+ Add New User';
$html .= ' || Delete User';
$html .= '<div id="div-data">';
$html .= '<table border="1" width="300px">';
$html .= '<tr>';
$html .= '<th width="5px"><input type="checkbox" id="cb-checkall"></th>'; //all
$html .= '<th> Users</th>';
//$html .= '<th>Delete</th>';
while($row = mysql_fetch_array($qry)) {
$html .= '<tr align="center">';
$html .= '<td><input type="checkbox" name="checkall" class="checkall" value="'.$row['uid'].'"/></td>';
$html .= '<td>'.$row['fname'].'</td>';
$html .= '</tr>';
}
$html .= '</table>';
$html .= '</div>';
echo $html;
delete.php (with the use of implode)
$deleteid = implode(",",$_POST['checkall']);
$fname = mysql_escape_string($_POST['firstname']);
$sql = "DELETE from `bpo`.`users` (`uid` ,`fname`)
WHERE UID IN (".$deleteid.")";
My data doesnt go to delete.php. idk why. I need help on deleting the checked rows when I push delete button and automatically reflects to my database. The change have to appear on screen without refreshing (AJAX) and with the use of implode. Thank you.
In your form change name="checkall" to name="checkall[]" to allow for multiple values.
Keep in mind that if no options are checked $_POST['checkall'] will be undefined which will generate an Undefined index notice. Use isset or array_key_exists.
If $deleteid is empty (like array()) you query will be malformed, ie.
DELETE from `bpo`.`users` (`uid` ,`fname`) WHERE UID IN ()
// unexpected ) -- ^
This will give you an SQL error. IN clause may not be empty.
I have the following code:
while($row = mysql_fetch_array($result)){
$output_items[] = $row["title"]; } // while
print(implode("\n", $output_items));
Which does what it says and splits the array with a new line for each item.
But how do I do the same and allow formatting with i.e. I basically want to say
foreach of the $output_items echo "<div class=whatever>$output_items</div> etc etc
Tearing my hair out with this!
Many thanks for all help
Darren
foreach ($output_items as $oi){
echo "<div class=whatever>$oi</div>";
}
doesn't work? or i did not get what you are searching for
Pretty simple, to make it easier to read I'd do something like this:
while($row = mysql_fetch_array($result))
{
echo '<div class="whatever">';
echo $row["title"];
echo '</div>' . "\n";
} // while
Although you could still do this with your original code pretty easily:
while($row = mysql_fetch_array($result)){
$output_items[] = '<div class="whatever">' . $row["title"] . '</div>'; } // while
print(implode("\n", $output_items));
Rather than implode() them all with line breaks, use string interpolation to add them together:
$out_string = "";
// Loop over your array $output_items and wrap each in <div />
// while appending each to a single output string.
foreach ($output_items as $item) {
$out_string .= "<div class='whatever'>$item</div>\n";
}
echo $out_string;