I'm trying to modify a script written in php, js with Yii. I simply need the value that I'm outputting to show up as a number without the column header. In this case, the number would be 20.
I'm using formulas.php to get the value that I need:
public function getMarkup()
{
$mid=$this->getMerchantID();
$DbExt=new DbExt;
$stmt="SELECT markup FROM
{{merchant}}
WHERE
merchant_id='$mid'
LIMIT 0,1
";
if ( $res=$DbExt->rst($stmt)){
return $res[0];
}
return false;
}
Then I'm calling the value on another page as:
<?php
$anArray2=Yii::app()->functions->getMarkup();
echo json_encode($anArray2);
?>
This returns:
{"markup":"20"}
I tried changing it to:
<?php
echo Yii::app()->functions->getMarkup();
?>
And that returns:
Array
I'm trying to get it to return with just 20 (the value of the column) without the column header.
<?php
echo Yii::app()->functions->getMarkup()["markup"];
?>
Is that what you're looking for?
Related
I have been trying to get my head around how to approach this problem. I am writing a web page book library with the categories ('nodes') as MySQL records. I want to print the list of categories at each level, starting at the highest level, and then allow the user to select a category to travel deeper into the library. The PHP codes runs a saved procedure in MySQL:
//loop the result set
while ($row = mysqli_fetch_array($result)) {
if ($row[1] <> 0) {
echo $row[0];
echo "<br />";
} else { // the first row is just the heading
"</strong>Category: ";
echo $row[0];
echo " : <br />";
}}
Because there are only two test categories, this produces output:
Books :
Nature
Children's Books
However, I want to be able to create an onclick event over 'Nature' and 'Children's Books' so the user can select a category and drill down t the next level via a php function. I can convert the php output into html eg:
<?= "<p>{$row[0]}</p>" ?>
but I can't see how I can identify the row in an onclick event to pass a parameter to the function. Perhaps I need to have a completely different approach?
Add an onclick attribute to the element that calls a JavaScript function that does what you want.
<?= "<p onclick='someFunc({$row['id']})'>{$row[0]}</p>" ?>
Replace id with the actual name of the column containing the ID of the row in the table. someFunc() can use that ID to look up information in an array or object, or send an AJAX request.
Case :
Hi all, actually i use POST and GET method in many times and they're work fine. But this time it isn't. I use POST TYPE form and then do PHP While (Looping) to generate some HTML attribute, i also give name to these HTML attribute (like input field etc). But second script (different page name), just won't detect it. It's like im using 1.php (where POST method applied) and 2.php (where GET method applied).
Experiment :
My experiment with code is like this basically (just to show logic, not the actual code).
1.php
<?php
echo <form method="POST" action="2.php">
while (fetch) {
echo fetch
}
echo </form>
?>
2.php
<?php
$x = $GET['id'] // work fine
$x = $GET['html_attribute'] // result : unidentified index
$query = update tbl_transaction set x='$x' where id='$id'
$query -> execute();
?>
Real Code of 2.php
<?php
include 'config/user_session.php';
include 'config/config.php';
if(isset($_GET['id']))
{
$id=$_GET['id'];
$no_ref=$_GET['no_ref'];
$desc=$_GET['desc'];
$amount=$_GET['amount'];
$via=$_GET['slPayment'];
$date=$_GET['date'];
$cat=$_GET['cat'];
$subcat=$_GET['subcat'];
$query1=mysql_query("update tbl_transaksi set no_ref='$no_ref', desc='$desc', amount='$amount', via='$via', date='$date', cat='$cat', subcat='$cat' where id='$id'");
if($query1)
{
echo '<script language="javascript">';
echo 'alert("Transaction Edited.")';
echo '</script>';
echo "<script>setTimeout(\"location.href = 'edit_transaction.php';\",100 </script>";
}
}
?>
I tried to change GET with POST, the result still same. Unidentified Index.
Desired Output :
I want 2.php GET Function return value from 1.php attribute value. Form method already POST.
Where did i do wrong ? What code i should modify ?
Thank you Stackoverflow Community.
I have a personal message system in my website done simply with php/sql. Actually I am facing the trouble to display them using jquery. The db has as fields: message_id, message_from, message_to, message_topic, message_subject and message_status. The way I am showing the message_topic is repeating eight times the following:
echo '<table><tr><td>';
retrieve_msg_topic($result);
echo '</td></tr>'; //of course I won't make 8 tables!!!
the function called is:
function retrieve_msg_topic($result)
{
if($row = mysql_fetch_assoc($result))
{
echo $row['usernombre'];
$message_topic = stripslashes($row['message_topic']);
echo '<div id="msg'.$row['message_id'].'">';
echo $message_topic;
echo '</div>';
//this will return: <div id="msgN">message topic (title, commonly subject)</div>
}
} //end function retrieve msg topic
So far I have a list on a table with the last eight messages sent to the user. The following row is reserved for pagination (next/prior page) and, after that, another row showing the message I select from the list presented, like we see in Outlook. Here is my headache. My approach is to call another function (8 times) and have all of them hidden until I click on one of the messages, like this:
echo '<tr><td>';
retrieve_msg_content($result);
retrieve_msg_content($result); //repeat 8 times
echo '</td></tr></table>';
the function this time would be something like this:
function retrieve_msg_content($result)
{
if($row = mysql_fetch_assoc($result))
{
echo '<script type="text/javascript">
$(document).ready(function(){
$("#msg'.$row['message_id'].'").click(function(){
$(".msgs").hide(1000);
$("#'.$row['message_id'].'").show(1000);
});
});
</script>';
echo '<div class="msgs" id="'.$row['message_id'].'" style="display: none">'
.$row['message_subject'].
'</div>';
}
/* This function returns:
// <script type="text/javascript">
// $(document).ready(function(){
// $("#msgN").click(function(){
// $(".msgs").hide(1000);
// $("#N").show(1000);
// });
// });
// </script>
// <div class="msgs" id="N" style="display: none">Message subject (body of message)</div>
*/
} //end function retrieve msg content/subject
I could simply explain that the problem is that it doesn't work and it is because I do if($row = mysql_fetch_assoc($result)) twice, so for the second time it doesn't have any more values!
The other approach I had was to call both the message_topic and message_subject in the same function but I end up with a sort of accordion which is not what I want.
I hope I was clear enough.
The easiest way to fix your troubles would be to copy the results of the MySQL query into an array
while($row = mysql_fetch_assoc($result)) {
$yourArray[] = $row;
}
And then use that to build your tables.
edit: What I meant was more along the lines of this:
while($row = mysql_fetch_assoc($result)) {
$yourArray[] = $row;
}
echo '<table>';
foreach($yourArray as $i) {
retrieve_msg_topic($i);
}
echo '<tr><td>';
foreach($yourArray as $i) {
retrieve_msg_content($i);
}
echo '</tr></td></table>';
And then removing everything to do with the SQL query from those functions, like this:
function retrieve_msg_topic($result) {
echo '<tr></td>'$result['usernombre'];
echo '<div id="msg'.$result['message_id'].'">';
echo stripslashes($result['message_topic']);
echo '</div><td></tr>';
}
Right now you're doing some weird key mojo with ret[0] being the topic and $ret[1] being the message, which isn't a good practise. Also, I don't see the declaration of $i anywhere in that code.
The error suggests that the result is empty or the query is malformed. I can't be sure from the code I've seen.
A few other notes: it seems weird that you're using stripslashes() on data that's directly from the DB. Are you sure you're not escaping stuff twice when inserting content into the DB?
Always use loops instead of writing something out x times (like the 8 times you said in your question). Think of a situation where you have to change something about the function call (the name, the parameters, whatever). With loops you have to edit 1 place. Without, you need to edit 8 different places.
BTW, another solution to this problem would be using AJAX to load content into the last cell. If you're curious, I could show you how.
more edits:
For AJAX, build your message list as usual and leave the target td empty. Then, add a jQuery AJAX call:
$('MSG_LIST_ELEMENT').click(function() {
var msgId = $(this).attr('id').replace('msg','');
$.get(AJAX_URL+'?msgID='+msgId,function(data) {
$('TARGET_TD').html(data);
})
});
Replace the capitalized variables with the ones you need. As for the PHP, just echo out the contents of the message with the ID $_GET['msgID'].
However, make sure you authenticate the user before echoing out any messages, so that someone else can't read someone's messages by switching the id number. Not sure how authentication works on your site, but this can be done by using session variables.
I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.
Surprisingly can't find anything on this... basically I call my custom product attributes on the product view page like this:
Men / Women: <?php echo $this->htmlEscape($_product->getmenwomen()) ?>
This displays the men/women attribute fine, but these are optional values, so if a product doesn't have a value for this particular attribute, the line is still displayed, just with no value:
Men / Women:
I'd like that line to not show at all if there is indeed, no value for a product. Any ideas?
You have to check if the value of getmenwomen() really contains something you expect it to contain (eg men/women) before printing it. In this example i presume that anything but whitespace is a valid value.
$menwomen = $_product->getmenwomen();
if (trim($menwomen)) {
echo "Men / Women: ".$this->htmlEscape($menwomen);
}
Not 100% on this because I am not sure if getmenwomen() will return false if its empty
If nothing is to be returned, by default it should return false.
<?php
if ($var = $this->htmlEscape($_product->getmenwomen()) {
echo "Men / Women: " + $var;
}
?>
You mean you want to display everything if there is a value and nothing if there isn't? Just add a simple conditional:
if (!empty($this->htmlEscape($_product->getmenwomen())))
echo 'Men / Women: '.$this->htmlEscape($_product->getmenwomen());
and that's it, you don't even need an else in there.