Putting Something Inside a Link with PHP - php

So I'm basically calling and returning an entire row from a mysql table using a while loop (which is working), but I'm trying to use the data that I call inside an html link, but I can't seem to get it to work.
Ideally, eventually it will just be a list of links with each person's individual name. I can return the list fine, but I can't seem to return the list with a link.
Here is my code that I feel should be working :(
<?php
require 'db/connect.php';
$result = $con->query('SELECT distinct name FROM mytable');
while($rows = $result->fetch_assoc())
{
echo ''$rows['name']'' , "</br>";
}
?>
Any help would be greatly appreciated!

Issue might be with your string concatenation. Try following code block
echo ''.$rows['name'].'';

echo ''. $rows['name']. '' , "</br>";
You just need to use . to concatenate strings together.

try this
echo ''.$rows['name'].'' , "</br>";
Should work just fine. Basically it's '.$row['name'].'

when concatenating strings with variables you have to use dot(.) like echo "string".$var; it will be invalid to write echo "string"$var; in your example you have ignored this point.

Related

Php and Html code within echo

I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>

Split Multiple Options in Single Attribute in Magento

I have multiple options in an attributes in Magento and when I call the attribute, all of the options show in a string and I would like to show them on separate lines (p tags or li's).
<?php echo $this->getChildHtml('spamodel') ?>
The above is my code, I think i need to use explode but I'm a newbie at php.
Thanks for any help.
<?php $spamodel = $this->getChildHtml('spamodel');
$spamodel_explode = explode(",",$spamodel);
echo $spamodel_explode[0];
?>
Here you can go with this code. you can get single the names with $spamodel_explode[0] and if you want to get all the names than you can use forloop
Here You can able to get the answer
<?php
$variable = $this->getChildHtml('spamodel')
$variable_exp = explode(",",$variable)
foreach($variable_exp as $var){
echo $var;
}
?>
In above coding you can explode based on your requirement.Just replace the ',' tag and get the results based on your requirement.
Find the below link for more details.
http://www.brandammo.co.uk/output-magento-custom-attributes-to-front-end-from-multi-select-dropdowns/

preg_replace of php code

I am writing an application that will look at a single record, obtain values from about 12 flags (0 or 1), look up those flags against a status table (in MySQL) and return a variable called $status_message which is in that table.
In this table I need to have hyperlinks (working fine) but also echo some variables, i.e.
You have no bids for {{$row->_item_name}}
or
View this item now by clicking here
Now I need item name and the other example to be translated into <?php echo $row->_item_name; ?>
I have tried a preg_replace with the following:
<?php
$find = array('/{{/', '/}}/');
$replace = array('<?php echo ', ' ?>');
echo preg_replace($find, $replace, $status_message);
?>
but this is not working.
Can anyone advise how I can get the desired result and 'echo' the variable in the MySQL field?
Had a brainwave. Much simpler,
instead of $row->_item_name I just put {{itemname}} in the string. I then use the following code:
<?php
$message_buyer = str_replace('{{itemname}}', $row->_item_name , $message_buyer);
echo $message_buyer;
?>
so no need to have <?php calls in the string at all.

Add PHP variable inside echo statement as href link address?

I'm trying to use a PHP variable to add a href value for a link in an echo statement.
Here's a simplified version of the code I want to use. I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.
$link_address = '#';
echo 'Link';
Try like
HTML in PHP :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like
PHP in HTML :
Link
you can either use
echo 'Link';
or
echo "Link';
if you use double quotes you can insert the variable into the string and it will be parsed.
Basically like this,
<?php
$link = ""; // Link goes here!
print "Link";
?>
as simple as that: echo 'Link';
You can use one and more echo statement inside href
Link
link : "/profile.php?usr=firstname&email=email"
This worked much better in my case.
HTML in PHP: Link
The safest way to generate links in PHP is to use the built-in function http_build_query(). This function is very easy to use and takes an array as an argument.
To create a dynamic link simply echo out the result of http_build_query() like so:
$data = [
'id' => $id,
'name' => $name
];
echo 'Link';
If you want to print in the tabular form with, then you can use this:
echo "<tr> <td><h3> ".$cat['id']."</h3></td><td><h3> ".$cat['title']."<h3></</td><td> <h3>".$cat['desc']."</h3></td><td><h3> ".$cat['process']."%"."<a href='taskUpdate.php' >Update</a>"."</h3></td></tr>" ;

Can't show the output in html

I have a function wich prints a number of credits:
<?php
function selectCredits()
{
include 'sqlvars.php';
$con = mysql_connect("localhost","bbbb","bbbb");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bbbb", $con);
$result = mysql_query($selectCredits);
while($row = mysql_fetch_array($result)){
echo $row['credits'];
}
}
?>
When I'm calling it from a php file by using:
<?php
include 'sql.php';
selectCredits();
?>
I can see the output, but when I'm inside of a HTML document I can't get the result using this code:
<center><b>Your credits: </b></center> <?php include 'sql.php'; selectCredits(); ?>
The output is always: Your credits: without the query result.
For sure I'm missing somthing really small. I'm not a php guy, but I'm willing to learn it, already lost an hour without any success.
If you really want html files to execute php code, try to add this line to htaccess:
AddType application/x-httpd-php .html
check your query statement '$selectCredits'. check table name and attribute name-'credits'. The $row['credits'] is not getting its value.
You have a problem in your code. You have written
while($row = mysql_fetch_array($result)){
echo $row['credits'];
}
You have used mysql_fetch_array and you have echoed $row with attribute value directly. If you are using mysql_fetch_array, then use $row[0], $row[1] or $row[2] etc. All the attributes come as an array. Thats why you should treat it as array index value. So, if "credit" attribute is at the 5th column in your database, then you should echo it as
echo $row['4'];
If you use mysql_fetch_assoc() instead of mysql_fetch_array, then you can write the attribute name directly.
E.g. then you can write
echo $row['credits'];
I hope, this will solve the issue. Please ask me if you have any further questions.
You should use a JavaScript tag to implement the PHP file like this:
<script type="text/javascript" src="credits.php"></script>
credits.php would have to output:
echo "document.write('" . $row['credits'] . "');";
Simple :)

Categories