Hi straight forward question really. I have a search function in php that prints out the required information from a data base. But it prints it out as one word. I don't want a line break...just a space between words. I've googled and checked this forum for answers but can't seem to find any.
The code works and does as it is required but it doesn't look neat.
Instead of: ID Job Title Job Description Job location Job Category
it looks like this:
IDJobTitleJobDescriptionJoblocationJobCategory
This is part of my php code.
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo
'<p>'
. $results['id']
. $results['job_title']
. $results['job_description']
. $results['job_location']
. $results['job_category']
. '</p>';
Please note I want it in one line, not line breaks. Thanks.
You have to echo the space like this .' '.
Or in your Case just replace your code with this.
echo
'<p>'
. $results['id']
.' '. $results['job_title']
.' '. $results['job_description']
.' '. $results['job_location']
.' '. $results['job_category']
.' '. '</p>
echo '<p>'
.$results['id'] . ' '
. $results['job_title'] . ' '
. $results['job_description'] . ' '
. $results['job_location'] . ' '
. $results['job_category'] . ' '
. '</p>';
Related
I want to separate several shortcodes by comma. Currently line is
<?php echo do_shortcode('[bangla_date]' . ',' . '[bangla_time]' . '.' . '[bangla_day]'); ?>
The output displayed on the website is a single line with no space in betwwen the shortcodes. Please help.
Try simply adding spaces:
<?php echo do_shortcode('[bangla_date]' . ', ' . '[bangla_time]' . ', ' . '[bangla_day]'); ?>
(',' => ', ')
Im echoing some data from a database using PHP. However the data is too close together and needs a space in between each one.
while($book = mysql_fetch_array($books)) {
echo '<div>'
.$book['title']
.$book['author']
.$book['genre']
.$book['price']
.$book['availability']
.'</div>';
}
Is their a way to print a break maybe after each one to give a space.
Cheers
You can print it as html entity :
echo '<div>'
.$book['title'] . ' '
.$book['author'] . ' '
.$book['genre'] . ' '
.$book['price'] . ' '
.$book['availability']
. '</div>';
yes, and the answer is shown below
while($book = mysql_fetch_array($books)) {
echo '<div>'
.$book['title']." "
.$book['author']." "
.$book['genre']." "
.$book['price']." "
.$book['availability']." "
.'</div>';
}
To add my five cents )
while($book = mysql_fetch_array($books)) {
echo "<div>{$book['title']}
{$book['author']}
{$book['genre']}
{$book['price']}
{$book['availability']}</div>";
}
You may use the curly syntax in the double quoted strings:
echo "<div>{$book['title']} {$book['author']} {$book['genre']} {$book['price']} {$book['availability']}</div>";
When a string is specified in double quotes or with heredoc, variables
are parsed within it.
I want to allow a user to search a table by selecting a field name from a dropdown list, then entering the term to search for in that field. The problem I'm having is that some fields are strings and some are numbers. When I construct the WHERE clause from the $_GET variables, I don't know how to delimit the search term because I can't come up with a good way to determine if the field selected by the user is numeric or string.
This is the search form:
<form action="<?php $_SERVER['PHP_SELF'] ?>">
<label for="search_field">Search:</label>
<select name="search_field">
<?php
$fields = $res->fetch_fields();
foreach($fields AS $f) {
echo '<option value="' . $f->name . '">' . $f->name . '</option>\n';
}
?>
</select>
<label for="search_for">For:</label>
<input type="text" name="search_for" />
<input type="submit" value="Search" />
</form>
The php file processes the search variables like this:
if(isset($_GET['search_field']) and isset($_GET['search_for'])) {
$sql_where = "AND " . $_GET['search_field'] . ' = ' . $_GET['search_for'] . ' ';
}
$sql = $sql . $sql_where;
The query works fine for numeric fields, but not string fields. I could put quotes around the search_for term, but then numeric fields wouldn't work. There has got to be a way to do this. Any ideas? Thanks.
Just use is_numeric function to determine if its an number or string:
if(isset($_GET['search_field']) and isset($_GET['search_for'])) {
if(is_numeric($_GET['search_for']))
$sql_where = 'AND ' . $_GET['search_field'] . ' = . $_GET['search_for'] . ';
else{
$sql_where = 'AND ' . $_GET['search_field'] . ' = ' . '.$_GET['search_for'] . ' ';
}
}
You can check if $_GET['search_for'] is numeric in php using is_numeric
if(isset($_GET['search_field']) and isset($_GET['search_for'])) {
$sql_where = "AND " . $_GET['search_field'] . ' = ' .
(is_numeric($_GET['search_for']) ? $_GET['search_for'] : '"' .
$_GET['search_for'] . '"' ) . ' ';
}
Also if you always delimit the search_for with quotes it will work with numeric values you can try it. I just wanted to show you the is_numeric function as well
You know your code has security problems, don't you?
But to solve your problem just do
$search_for = is_numeric($_GET['search_for'])?$_GET['search_for']:"'".addslashes($_GET['search_for'])."'";
$sql_where = 'AND `' . $_GET['search_field'] . '` = ' . $search_for . ' ';
I am trying to add another field 'field2' that can also be searched and returned in the search results.
$extraSQL .= ' AND a.'.$db->nameQuote('field1').' LIKE ' . $db->Quote( '%' . $search . '%' ) . ' ';
If I change 'field1' in above code to 'field2' then field2 does work but I am having trouble adding both to the above code.
Sure its something very simple that I am missing but any help would be appreciated.
Thanks.
Judging from your code I'd say something like:
$extraSQL .= ' AND (a.' . $db->nameQuote('field1') . ' LIKE ' . $db->Quote( '%' . $search . '%' ) . ' OR a.'.$db->nameQuote('field2').' LIKE ' . $db->Quote( '%' . $search . '%) ) ';
If you want either of the fields to match, or change OR to AND if you want both fields to match.
hi friends why this php string error ?
echo '<div id="album_list">' . $i . ' ' . $v['album_name']. '</div>';
You have some missing single quotes.
echo '<div id="album_list">' . $i . ' ' . $v['album_name']. '</div>';
// you need a single quote here ^ ^ and here
You are missing a single-quote after album_pix/ and before the closing bracket.
echo '<div id="album_list">' . $i . ' ' . $v['album_name']. '</div>';
Single quote the string with the double quotes and attributes
Single space and concatenate with the period .
Change $var['key'] to $var, or $var["key"]
I'd change your variable names to reduce the confusion. As someone said above syntax highlighting will turn all the strings one color, and the variables another. Stack Overflow even displays code as such.
<?php
$v_id = $v['id'];
$v_album_name = $v['album_name'];
echo '<div id="album_list">' . $i . ' ' . $v_album_name . '</div>';
?>