I'm working on a database website currently and part of the project is to display the content of a database in a custom form using pixels to position the various fields.
I am however having trouble getting the code to work as the way I'm creating the form doesn't seem to work in a php tag, but It requires a WHILE loop in order to continue running until the database field is empty.
I'm only including the relevant code section so as to not clog up the post, all my coding is segmented so I can isolate bugs.
<?php
$usernameh = 20;
$units = "px";
for ($x=0; $x<=2; $x++) {
$pix=$value . $units;
<div style="position:absolute;left:10px;top:20px;width:100px;height:20px;z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>
$value += 150;
}
?>
The idea is to substitute the 20px on the first line of the divcommand with $pix so that with each iteration the value increases by a set amount and separates out the entries.
I'm very new to php coding, as in only really started 2 weeks ago. I'm sure there is a simple solution to this but I'm not sure what question to ask Google to get the response I'm after.
I hope my problem makes sense. The database is in MySQL but all that is fine, its just the formatting I'm struggling with. Even without using a variable in the formatting code the script crashes while the php tag is in effect.
Can anyone offer any advice on where my problem is or suggest another alternative to this.
Thanks!
You need to break out of PHP ?> before HTML and then switch back to PHP <?php before more PHP code
$value = 20;
$units = "px";
for ($x=0; $x<=2; $x++) {
$pix = $value . $units;
?>
<div style="position:absolute;left:10px;top:<?php echo $pix ?>;width:100px;height:<?php echo $pix ?>;z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>
<?php
$value += 150;
}
Or for the HTML you can echo it from PHP:
echo '<div style="position:absolute;left:10px;top:' . $pix . ';width:100px;height:' . $pix . ';z- index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>';
Related
PHPmailer will not let me add an embedded image inside a foreach loop the path is correct but for some reason it is not adding the image i have checked other sorces on here but with no luck the images are embedding fine when out of the loop but it is just when i call them by using variable names eg the way i need to inside a for each loop as the images will be dynamic it will not embed any help appreciated.
The path looks all good but it just will not run in the loop?
Please let me know if you need to see any more code i just did not want to include irrelevant code as i know the message is already sending fine it is just this bit that i have a problem with. I have the table working in html that is not being sent via email if that is of any use outputting the images correctly in the foreach loop
<tbody>'.
'<span style="display:none">'. $i = 1;' .</span>'.
$total_quantity_count = 0;
$grand_total = 0;
foreach($items_purchased_array as $item){
if (!$mail->addEmbeddedImage(getcwd() . '/' . $item['image'],'product_pic'.$i)) {
echo 'Failed to attach '. getcwd() . '/' . $item['image'],'product_pic'.$i;
}
</tbody>
Here was what i did to fix this
if (!$mail->addEmbeddedImage(getcwd().'/'.trim($item['image']),trim('product_pic'.$i))){
echo 'Failed to attach '.getcwd().'/'.$item['image'],('product_pic'.$i);
I've been playing around with a php input function that will build a html page for a game, something like a wiki. All is going fine and dandy but when trying to build an array into a string it's passing back some funny errors.
It's happening to every one that involves an array and I've snipped some code out that works independently to save me time, but just can't figure it out. I'm using the Linux Terminal to run the script and when I set it to echo inside the foreach loop it does it just fine, it just won't when I try building it into a HTML file.
Here is the input script:
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
$facInputRankString = "";
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
foreach ($facInputRanks as $facInputRankList) {
$facInputRankString .= $facInputRankList.PHP_EOL;
}
Then I'll build it into a multi-line echo (rather than appending every single block of code):
$facBuildPage = <<<EOT
<?php
\$facRankLimit = '$facInputRankLimit';
\$facRanks = '$facInputRankString';
include('faction2.html');
?>
EOT;
The variables with the backslashes will then be built into "$facFileName.php" and will be a set of variables inputted through this script (input2.php), which will each also include the same html page.
To top it off I'm getting really weird results... if I create 5 ranks, each as "1 2 3 4 5", I actually get "1 2 3" with two vertical linebreaks in between.
Edit: Snipped some of it, I didn't want to risk not adding enough info but it turns out I added a bit much.
For instance, the input I'm giving is:
$facInputRankLimit = 5;
$facInputRanks[1] = 1; -> $facInputRanks[5] = 5;
But it prints:
1<br><br>2<br><br>3
#Deepkak - The relative code in faction2.html is:
<div class="fpDivisions fpBox">
<span class="header">Ranks</span><br>
<?php
$facRankCount = 0;
while ($facRankCount < $facRankLimit) {
echo $facRanks[$facRankCount].'<br>';
$facRankCount++;
}
?>
</div>
I Hope this will solve the issue
<?php
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
$array = [];
foreach ($facInputRanks as $facInputRankList) {
$array[] = "'".$facInputRankList."'";
}
$facInputRankString = "[".implode(",",$array)."]";
$facBuildPage = <<<EOT
<?php
\$facRankLimit = '$facInputRankLimit';
\$facRanks = $facInputRankString;
include('faction2.html');
?>
EOT;
echo $facBuildPage;
I've created a workaround for this, incase anyone needs any help in the future. It was as simple as echoing it from the HTML page, rather than creating a loop that runs through both the PHP script and the HTML code.
PHP input code:
echo "\nHow many ranks were there (number)?:\n";
$facInputRankLimit = readline();
echo "Please read carefully and supply the ranks in descending order (HIGHEST > LOWEST):\n";
$facInputRankCount = 0;
while ($facInputRankCount < $facInputRankLimit) {
echo "Enter a rank:\n";
$facInputRanks[$facInputRankCount] = readline();
$facInputRankCount++;
}
$facInputList = implode("<br>", $facInputRanks);
PHP output code:
$facBuildPage = <<<EOT
<?php
\$facRanks = '$facInputList';
include('faction2.html');
?>
EOT;
file_put_contents("test/$facFileName.php", $facBuildPage);
HTML output code:
<div class="fpDivisions fpBox">
<span class="header">Ranks</span><br>
<?php echo $facRanks; ?>
</div>
I am working with PHP and My SQL to get data from an SQL database and show it in 2 columns.
I have figured out how to do it with the code as below. However I am working with a complex HTML template and don't want to echo every single HTML line to get it work - It will be very complicated to read and fix if there is an issue!
<table>
<?php
$i = 0;
while($row = $result->fetch_assoc()) {?>
<?php if (++$i % 2 != 0) echo "<tr>";?>
<?php echo "<td>" .$row['fname']."</td>"; ?>
<?php if ($i % 2 == 0);
} if ($i % 2 != 0); ?>
<td></td></tr>
</table>
I have simplified the template as below. Here I am 'echoing' within the HTML which I find easier to read and understand:
<div class=container>
<div id="fname"><a>First Name:</a><?php echo $row['fname'];?></div>
<div id="website"><a>Website:</a><?php echo $row['website'];?></div>
</div>
To summarize, is it possible to take my data from the database and show it in 2 columns without having to echo every single HTML line. Keeping my HTML structure similar to the above?
The column layout I am try to achieve is as below:
1 | 2
-----
3 | 4
-----
5 | 6
-----
I have not been able to find an example anywhere! Thanks!
There are advantages to writing HTML out in PHP script and there are also advantages to breaking into PHP from your HTML script. It really just depends on the circumstances.
For example if I am writing html that is generated from loops of has alot of variable inputs I will write the HTML inside the PHP. However, If I am writing a big chunk of HTML with only a few variables I will break into PHP from the HTML.
It really just depends on the circumstances.
That being said, when writing HTML from inside of PHP I will break it down and concatenate the HTML as much as possible. That way I am not breaking in and out of PHP to get the task accomplished. The more you code the easier that will get.
Below is how I would write your code. Notice I never broke in and out of PHP.
echo
'<table>';
$i = 0;
while($row = $result->fetch_assoc()) {
echo
'<tr>';
echo
'<td>' . $row['fname'] . '</td>' .
'<td>' . $row['lname'] . '</td>';
echo
'</tr>';
}
echo
'</table>';
Hope this helps.
You could do something like this in PHP, just another very simple option.
$str = '';
while($row = $result->fetch_assoc()) {
$str .= '<tr><td>'.$row['value1'].'</td><td>'.$row['value2'].'</td></tr>';
}
I don't know all the correct keys in the $row, but I think you get the idea.
And than the HTML would be like this:
<table>
<?php echo $str; ?>
</table>
Just a little hint. You can google on a MVC construction. This stands for Model View Controller and it is in my opinion the best way to keep your code organised.
My website consists of many products that are each contained in a div with the id content block. The link, image, background, description and price are all loaded from a mySQL table. My original plan was to save the below html code as a string and loop over the rows in the mySQL table filling the string I created with php/mySQL values.
I was wondering if I am going about this the right way, or is there a better way to create html code from php variables?
<div id="contentblock" style="background-image:url(images/$BACKGROUND.png);">
<div id="picture"><img src="$IMAGELINK"/></div>
<div id="description"><p>$DESCRIPTION</p></div>
<div id="price"><p class=price>$PRICE</p></div>
</div>
Firstly PHP is a template engine - in my experience template engines that layer ontop of PHP are only good for the simplest of cases and are easily outgrown.
Secondly the original code is as good as any method. At risk of stating the obvious to make it better abstract it into a function;
function output_block($BACKGROUND, $LINK, $IMAGELINK, $DESCRIPTION, $PRICE)
{
echo "<div id='contentblock' style='background-image:url(images/$BACKGROUND.png);'>
<div id='picture'><a href='$LINK'><img src='$IMAGELINK'/></a></div>
<div id='description'><p>$DESCRIPTION</p></div>
<div id='price'><p class=price>$PRICE</p></div>
</div>";
}
If you want to make it much better then adopt a framework, an entire admin config page is show below. All of the HTML glue is provided by the framework - the following code is real, but really to illustrate how a framework can provide a lot of the grunge work for you.
In the example below if I want to edit a single entity I'd change the TableViewEdit into a FormView and provide an instance of an entity rather than an iterable list.
$entity = new CbfConfig(); // Database entity
$page = new AdminWebPage("Site Configuration"); // Page for output
/*
* build the view
*/
$vil = new ViewItemList();
$col = &$vil->add(new ViewItem("description","Description"));
$col->get_output_transform()->allow_edit(false); // this field cannot be editted
$col = &$vil->add(new ViewItem("value","Value"));
$v1 = new TableViewEdit($entity, $vil,"admin_values"); // present as standard editable table
/*
* output the page
*/
$page->begin();
$iterable_list = CbfConfig::site_begin();
$page->add_body($v1->get_output($iterable_list,'admin_config'));
$page->end();
Id just have all my html code outside of php tags, then whereever I need a variable from php do as follows
<div id="description"><p><?php echo $DESCRIPTION; ?></p></div>
You can loop around non php code too. For example
<?php
for($i = 0; $i < 10; $i++) {
?>
<div id="description"><p><?php echo $i; ?></p></div>
<?php
} //end for loop
?>
Obviously this is just an example.
well if im without a template engine for somereason i usually do something like:
function partial($file, $args = array()) {
extract($args);
ob_start();
include($file);
return ob_get_clean();
}
Really, there are 3 ways of doing this. Use whichever is easiest for you in the context that you are using it in.
<?php
while(($row=mysql_fetch_assoc($result))!==false)
{
echo "<div>{$row['fieldName']}</div>";
}
?>
<?php
while(($row=mysql_fetch_assoc($result))!==false)
{
echo '<div>'.$row['fieldName'].'</div>';
}
?>
<?php
while(($row=mysql_fetch_assoc($result))!==false)
{
?>
<div><?= $row['fieldName']; ?></div>
<?php
}
?>
Is there a way I can use Jquery to insert '' tags after every three dynamically generated table cells so that I end up with a dynamic three column table?
Please excuse my lack of knowledge, I'm literally trying to write my first jquery script ever, so I know absolutely nothing. I know php and I have a table that has a loop within it that is dynamically creating <td></td> with the information inside each tag. In other words it is dynamically creating the table cells within a static <tr></tr> tag. The problem is that it keeps outputing tables without breaking them up into rows which leaves me with a bunch of columns. I've read other articles on this but none seem to have the exact same problem as I do, and I am still struggling to understand how to write custom Jquery code.
The php code is very long and is full of numerous if statements and other functions so I'm not going to post it here but just to make it a little simpler, I made miniature mockup of what I'm trying to do.
<table id="mytable" width="266" border="1" cellspacing="10" cellpadding="10">
<tr>
<?php
$x=0;
while (have_products($x)) {
echo '<td>' . somelongassfunction() . '</td>';
$x++;
if (fmod($x,3) == 0) {
echo '</tr><tr>';
continue;
}
if ($x==20){
echo '</tr>';
}
}
function somelongassfunction(){
return 'Hello';
}
function have_products($a){
return $a<=20;
}
?>
</table>
This code loops and dynamically adds table cells up to the limit I give it which would represent my database items. Every three rows, it adds either a <tr></tr> or just a </tr> depending on whether the loop continues or not. This creates a 3 column table. I can't apply this code for my script because it is a very long and complex script that has a lot of if statements and functions. There is no way of doing it like this without breaking the code or having to rewrite everything from scratch all over again.
Is there anyway I can append the tr tags dynamically with Jquery and how would I go about to applying this to?
The jQuery approach would be to loop through all of the tabs, and add them to newly created tags, which themselves are added to the html of the table. Roughly:
var thisCount=0;
var currenttag="<tr />";
var table=$("table");
$("td").each(function ()
{
if(thiscount==2)
{
table.appendChild(currenttag);
thisCount=0;
currenttag="<tr />";
}
currenttag.appendChild(this);
}
( this is just to give an idea, not intended as a formal JQ answer. If anyone wants to edit it so it works fully, feel free ).
You can use a selector to select every third row:
$('#table_id > tr:nth-child(3n)').whatever_function()
However if you are trying to append end /tr tags, try doing it in PHP using a counter that resets itself (this code should get you started):
echo "<tr>";
$x = 0;
$y = 0;
while (have_products($x)) {
echo '<td>' . somelongassfunction() . '</td>';
$y++;
if ($y == 3) {
$y = 0;
echo "</tr><tr>";
}
$x++;
}
echo "</tr>";