PHP/Codeigniter elseif not processing - php

I can't seem to get the elseif to process in the below php code. My database setup is noted below:
<?php // the following php is the bootstrapper for the friendship connector
$selectedId = $_REQUEST['id'];
$myuserid = $this->session->userdata('userid');
$friendshipQuery = $this->db->query("SELECT * FROM friends");
$row = $friendshipQuery->row();
foreach ($friendshipQuery->result() as $row) {
if ($myuserid == $selectedId) { ?>
<script type="text/javascript">
$(document).ready(function() {
$("#addFriend").hide();
});
</script>
<?php } elseif ($row->relationType == 'requested') { ?>
<script type="text/javascript">
$(document).ready(function() {
alert("hello");
$("#addFriend").replaceWith(<input type="submit" value="Accept Friend" style="width: 95px; height: 28px" class="button1" />);
});
</script>
<?php } } ?>

Your problem is right here:
$friendshipQuery = $this->db->query("SELECT * FROM friends");
$row = $friendshipQuery->row();
foreach ($friendshipQuery->result() as $row) {
Your assigning the query as a ->row() element. Take that line out and on the foreach just add this:
foreach($friendshipQuery->result() as $row)
That should solve it.

Have you checked the generated html? If it doesn't contain that part, it means that $myuserid == $selectedId is always true.

Related

Selection option in php loop

I have the code below and this code works, but only after clicking on selection option but code doesn't work when i change value when use arrow up and down.
I tried modification script, change option "click" to "change" but this solution doesn't work. Someone can help me ?
$select = $db_connect -> query("SELECT * FROM templates");
if($select -> num_rows > 0)
{
echo '<select id="subject" name="subject" class="form-select">';
while($row = $select -> fetch_assoc())
{
echo '<option id="'.$row["id"].'" value="'.$row['template_subject'].'">'.$row['template_subject'].'</option>';
?>
<script>
$("#subject #<?php echo $row['id']; ?>").on('click', function(){
if((this.id = "<?php echo $row['id'];?>") && (this.id != 1))
{
$("textarea").html("<?php echo $row['template_text']; ?>");
$("input[name='new_subject']").hide();
}
else
{
$("textarea").html("");
$("input[name='new_subject']").show();
}
});
</script>
<?php
}
echo '</select>';
}
Your problem is in the Javascript code.
<script>
$("#subject").change( function(){
var text = $( "#subject option:selected").val();
var id = $(this).children(":selected").attr("id");
if(id != 1)
{
$("textarea").html(text);
$("input[name='new_subject']").hide();
}
else
{
$("textarea").html("");
$("input[name='new_subject']").show();
}
});
</script>
remove the script from the while loop , put it at the end before </body> tag.

Open new window for multiple edit records

What I have here is a table that displays record from my database. It also generates dynamic checkboxes along with the fetched record. What I need to do is to open a new window using window.open, instead for the page to go to editor.php. Any help will be gladly appreciated.
MySQLi Query
while ($row = $result1->fetch_assoc()) {
<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="' . $row['id'] . '"'.($row['pr'] == ""?"disabled ":"").' style="cursor:pointer;" class="checkbox"></td>
}
Javascript
<script type="text/javascript" language="javascript">
function setCheckboxes3(act) {
$('.checkbox').not(':disabled').prop('checked', act == 1 ? true : false);
//or $('.checkbox:not(:disabled)').prop('checked', act == 1 ? true : false)
}
</script>
<script>
jQuery(function ($) {
$('a.button.edit, a.button.remove').click(function () {
if ($('input[name="checkbox[]"]:checked').length == 0) {
return;
}
if ($(this).hasClass('edit')) {
if (!confirm('Edit this Record(s)?')) {
return
}
} else {
if (!confirm('WARNING !!! All Purchase Order that included in this Record(s) will be deleted.')) {
return
}
}
var frm = document.myform;
if ($(this).hasClass('edit')) {
frm.action = 'editpr.php';
}
if ($(this).hasClass('remove')) {
if (!confirm('Are you sure want to continue?')) {
return
}
}
frm.submit();
})
})
</script>
HTML
<a class="button edit" style="cursor:pointer;"><span><b>Edit</b></span></a>

Checkboxes, javascript and php. Not loading results

I have a javascript code that reads from a set of checkboxes and loads a php in case one or more checkboxes are checked. This php will load only the items that are selected.
However, now I click on the checkboxes and nothing happens. I am not sure if the issue is in the php or in the javascript.
Here are the codes:
CHECKBOXES:
function echoCheckboxSet($header, $divClass, $columnName, $setName) {
include ("../commonItems/connection.php");
$checkboxes = $con -> prepare("SELECT DISTINCT $columnName FROM item_descr ORDER BY $columnName ASC");
$checkboxes->execute();
<?php
while ($box = $checkboxes->fetch(PDO::FETCH_ASSOC)):
$boxColumnName = str_replace('_',' ',$box[$columnName]);
?>
<input type='checkbox' class='regularCheckbox' name='<?php echo $setName; ?>' value='<?php echo $box[$columnName]; ?>' />
<font class='similarItemsText'><?php echo $boxColumnName; ?></font>
<br />
<?php
endwhile;
echoCheckBoxSet("COLOR", "colors", "color_base1", "color");
echoCheckBoxSet("PRICE", "prices", "price", "price");
?>
JAVASCRIPT
$(function() {
$("input[type='checkbox']").on('change', function() {
var boxes = [];
// You could save a little time and space by doing this:
var name = this.name;
// critical change on next line
$("input[type='checkbox'][name='"+this.name+"']:checked").each(function() {
boxes.push(this.value);
});
if (boxes.length) {
$(".loadingItems").fadeIn(300);
// Change the name here as well
$(".indexMain").load('indexMain.php?'+this.name+'=' + boxes.join("+"),
function() {
$(".indexMain").fadeIn('slow');
$(".loadingItems").fadeOut(300);
});
} else {
$(".loadingItems").fadeIn(300);
$(".indexMain").load('indexMain.php', function() {
$(".indexMain").fadeIn('slow');
$(".loadingItems").fadeOut(300);
});
}
});
});
INDEXMAIN (indexMain.php)
include('../commonItems/connection.php');
if ($colors != '')
{
$colors = explode(' ', $colors);
$parameters = join(', ', array_fill(0, count($colors), '?'));
$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 IN ({$parameters})");
$items ->execute($colors);
$count = $items -> rowCount();
}
while($info = $items->fetch(PDO::FETCH_ASSOC))
{
......... echo everything....
At some point the code was working fine... but now it is not...
Thanks!
indexMain.php is not getting the passed variables.
Adding this at the top made it work:
$colors = $_GET['color'];

php jquery selecting the specific checkbox inside the while fetch_array

I have a problem selecting specific checkbox and putting higlight css for its div inside the mysql_fetch_array
here's my code
$count=1;
$query = mysql_query('SELECT * FROM thread');
while($row = mysql_fetch_array($query))
{
echo "<div class='row".$count."'><input type='checkebox' class='chk_box".$count."'> ".$row['title']."</div>";
$count++;
}
<script>
var count= "<?php echo $count?>";
for(var y=1;y<=count;y++){
$('.chk_box'+y).click(function() {
if(this.checked) {
$('.row'+y).addClass('backcolor');
}
else{
$('.row'+y).removeClass('backcolor');
}
});
}
</script>
Fixed som typos, added a DOM ready function, and changed the jQuery a bit, try that ?
$count=1;
$query = mysql_query('SELECT * FROM thread');
while($row = mysql_fetch_array($query)) {
echo "<div class='row".$count."'><input type='checkbox' class='chk_box".$count."'> ".$row['title']."</div>";
$count++;
}
<script type="text/javascript">
$(function() {
$('[class^="chk_box"]').on('click', function() {
$(this).closest('div').toggleClass('backcolor', this.checked);
});
});
</script>​​​​​​​​​​
as a sidenote, attaching event handlers inside for loops is usually not a very good idea.

Jquery autosuggest not working

Hey guys, I'm using jQuery's autosuggest plugin by using php to get the data. But it doesn't seem to work since I always get: No Results Found, even though I'm sure there are results:
Here's the php code:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
$input = mysql_escape_string($_GET["q"]);
$data = array();
$mysql=mysql_connect('localhost','***','***');
mysql_select_db('jmtdy');
$query = mysql_query("SELECT * FROM users WHERE username LIKE '%".$input."%'");
while ($row = mysql_fetch_assoc($query)) {
$json = array();
$json['value'] = $row['id'];
$json['name'] = $row['username'];
$data[] = $json;
}
header("Content-type: application/json");
echo json_encode($data);
?>
And the script:
<script >
$(document).ready(function () {
$("#suggestedfriend").autoSuggest("suggestedf.php");
});
</script>
<script >
$(document).ready(function () {
$("#suggestedfriend").autoSuggest(
"suggestedf.php",
{
selectedValuesProp: "value",
selectedItemProp: "name",
searchObjProps: "name"
});
});
</script>
Add the above parameters, it will start to work :)
Just look at data, that server sends you back. If you use firefox you can watch it in network tab of firebug, or if you use chrome see it in resources.
The header must be in top of the file, right after the
<?php
header('Content-type: application/json');
include_once 'resources/dbconn.php';
$term = $_REQUEST['term'];
$query = "SELECT * FROM cds WHERE titel LIKE '%$term%'";
$result = $mysqli->query($query);
$arr = array();
while ($obj = $result->fetch_array()) {
$arr[] = $obj;
}
//for jsonp echo '('.json_encode($arr).')';
echo json_encode($arr);
?>
The JS/jQuery string
<script type="text/javascript">
$(function() {
var cache = {},
lastXhr;
$("#exercise").autocomplete({
minLength: 2,
source: function(request, response) {
var term = request.term;
if (term in cache) {
response(cache[term]);
return;
}
lastXhr = $.getJSON("json_Search.php", request, function(data,status,xhr) {
cache[term] = data;
if (xhr === lastXhr) {
response(data);
}
});
}
});
});
</script>

Categories