PHP Concatenation for displaying from the db - php

How do I make this line of code work?
echo $row['Headings'.' '.'Contents']; //
I want to display what's in the 'Headings' column from the database, then a page break, then what's in the 'Contents' column in the db. Please help.

Try this
echo $row['Headings'] .'<br />'. $row['Contents'];

echo $row['headings'] . '&nbsp' . $row['contents'];
Should do the trick.

You can use it as :
$result = mysql_query('select concat(Headings, Contents) as final_string from table_name');
while ($row = mysql_fetch_assoc($result)){
echo $row['final_string'];
}
Please correct me if I am wrong.

Set glue
$glue = "<br />"; // or even what you want
Store it in one variable
$variable = $row['Headings'];
$variable .= $glue;
$variable .= $row['Contents'];
== OR implode by Array ==
$variable = implode($glue, Array($row['Headings'], $row['Contents']));
And then display
echo $variable;

Just do
echo "{$row['Headings']}<br />{$row['Contents']}";
Or the way I should do it
$headings = $row['Headings'];
$contents = $row['Contents'];
echo $headings.'<br />'.$contents

Try this way :
echo $row['Headings'];
echo '<br />';
echo $row['Contents'];

Related

why do I get only 3 return values by using a json link:

there should be many more return values?
print_r($data);
shows that there is much more to be displayed.
Thanks for any hints,
Stefan
<?php
$data = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-DOGE&type=sell'),TRUE);
//print_r($data);
$ncount = COUNT( $data );
for($i=0;$i<$ncount;$i++){
echo $data['result'][$i]['Quantity'] .'<br />';
}
?>
$ncount should be the count() of $data['result'];
Try this:
<?php
$data = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-DOGE&type=sell'),TRUE);
$ncount = count($data['result']);
for($i=0;$i<$ncount;$i++){
echo $data['result'][$i]['Quantity'] .'<br />';
}
?>
While not a definite answer, you're running count on $data, but then displaying $data['result']. So if the root array only contains 3 values, you'll only go through the loop 3 times. Instead, try a foreach:
foreach ($data['result'] as $result){
echo $result['Quantity'] .'<br />';
}
You are making things WAY too difficult .. Set result to data['result'] and iterate through that
<?php
$data = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getorderbook?market=BTC-DOGE&type=sell'),TRUE);
$result = $data['result'];
foreach($result as $item){
echo $item['Quantity'] . '<br />';
}
?>

php echo statement inside $output

I have the following php code:
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
return $output;
For some reason though, the value of $skizzar_double_width_size is not being added into the $output - is there a way to echo a value in an output variable?
As #Rizier123 mentioned, ensure you initialise any string variables before trying to append to them.
$var = '';
$var .= 'I appended';
$var .= ' a string!';
I would also like to strongly discourage you from using inline styles as well as generating them with inline PHP. Things get very messy very quickly.
In a situation like this you need to check that all the variables you are using in the calculation are valid before you panic.
So try
echo 'before I use these values they contain<br>';
echo '$masonry_item_width = ' . $masonry_item_width . '<br>';
echo '$masonry_item_padding = ' . $masonry_item_padding . '<br>';
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
echo 'after moving the fields to an unnecessary intemediary field<br>';
echo '$skizzar_masonry_item_width = ' . $skizzar_masonry_item_width . '<br>';
echo '$skizzar_masonry_item_padding = ' . $skizzar_masonry_item_padding . '<br>';
echo '$skizzar_double_width_size = ' . $skizzar_double_width_size . '<br>';
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
echo $output;
This should identify which fields are causing you problems.
Also while testing always run with display_errors = On It saves so much time in the long run.

double quotes inside single quotes in MYSQL result?

I'm trying to do something very basic but I can't figure out how.
basically i'm trying to convert the mysql result ($row) into the following format (literal strings):
"0784562627828" => "James",
"0786636363663" => "David",
I have all the data stored in the database and I can get them echoed on my page like so:
$phone = $row['phone'];
$name = $row['name'];
$list .=''.$phone.'';
echo $list;
could someone please advise on this?
Thanks
Just assign them inside an array like you normally would:
$array = array();
while(your fetch here) {
$array[$row['phone']] = $row['name'];
}
To check its contents, you can use var_dump($array) or print_r($array)
Or just straight up show them, like the one you formatted:
while(your fetch here) {
echo '"' . $row['phone'] . '"' . ' => ' . '"' . $row['name'] . '"' . '<br/>';
}
you mean something like this?
$list = array();
$list[$phone] = $name;
Can you do something like
$list = [];
foreach($rows as $row) {
$list[$row['phone']] = $row['name'];
}

Create a php file with the value of echo values

I have a piece of code like this:
$classVoHeader = 'class C'.toCamelCase($tableName,true).'Vo{';
$classVoFooter = '}';
$str ='public $table_map = array(';
$propertyStr = '';
foreach($columnInfos as $column){
$str.=$br.'\''.$column['Field'].'\' => \''.toCamelCase($column['Field']).'\',';
$propertyStr.=$br.'public $'.toCamelCase($column['Field']).';';
}
$str.=$br.');';
echo $classVoHeader.$br;
echo $str;
echo $propertyStr.$br;
echo $classVoFooter;
And I want to create a php file that have content is all of what it echoed.
Is it impossible?
Take a look at http://www.php.net/manual/en/function.ob-get-contents.php to save what is being printed into a buffer, then use http://www.php.net/manual/en/function.file-put-contents.php to save this string to a file
Strange question but, here you go:
$Result = '<?php ' . $classVoHeader.$br . $str . $propertyStr.$br . $classVoFooter . ' ?>';
$file = fopen("result.php","w");
echo fwrite($file,$Result);
fclose($file);
i dont know what your code is and it doesnt matter. With my answer you will have all the output that is produced between ob_start(); and ob_get_clean(); saved to the variable $the_output_of_code. Then just write this to a file.
<?php
ob_start();
// your code begins here
// [your piece of code here whatever]
$classVoHeader = 'class C'.toCamelCase($tableName,true).'Vo{';
$classVoFooter = '}';
$str ='public $table_map = array(';
$propertyStr = '';
foreach($columnInfos as $column){
$str.=$br.'\''.$column['Field'].'\' => \''.toCamelCase($column['Field']).'\',';
$propertyStr.=$br.'public $'.toCamelCase($column['Field']).';';
}
$str.=$br.');';
echo $classVoHeader.$br;
echo $str;
echo $propertyStr.$br;
echo $classVoFooter;
// your code ends here
$the_output_of_code = ob_get_clean();
$file = fopen("the_output.php","w");
fwrite($file,$the_output_of_code);
fclose($file);
?>

Whats wrong with my link?

I am trying to echo a array value in a link, but it is coming up in dreamweaver as an error, but I cant work out what I have done wrong, can anyone tell me what is wrong with this line please ?.
thanks :-)
echo '';
EDIT >>>>>>>>>>>>>>>>>>>>>>>>
THIS IS THE FULL CODE :
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
echo 'dssd';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
If I echo the cf_uid
echo $row->cf_uid;
this works fine and displays the unique id for each record next to it in the table, I just need to take that id thats is being echo'd and put in at the end of the link so that it looks like http://link&token=2626382837728 << (cf_uid)
FIXED !
Thanks for everyone's help on this work, I worked out what was wrong in the end, what I thought was an array didn't appear to be, this code worked in the end >>
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
$id = $row->cf_uid;
echo 'LINK';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
Try to separate the strings and the variables.
echo '';
update: If you get an error in this line the problem could be the line before!
alternatively you can try this
echo '';
This way you can just concat the string
echo "SOME NAME FOR THE LINK";
[edit based on updated post]
Use this:
echo 'dssd';
However, I must ask... is there an array variable $detail which has a key 'cf_uid'? And what syntax error are you getting (after you tried this)?
[edit based on comment]
Since it's $row and since it's an object:
echo 'dssd';

Categories