Jump to content

a little more array help...


dennismonsewicz

Recommended Posts

code i am working with:

 

<?php

/*ini_set ("display_errors", "1");
error_reporting(E_ALL);*/

include "includes/sql.php";

$id = 24;

$query = mysql_query("SELECT * FROM added_projects WHERE id = '$id'")or die(mysql_error());

$results = mysql_fetch_object($query);

$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
$checkbox_results = mysql_fetch_object($checkbox_qry);

$field = mysql_num_fields($checkbox_qry);

	for($i = 2; $i < $field; $i++) {
		$names = mysql_field_name($checkbox_qry, $i);			
		$title .= $names . "<br />";
		/*$nobreak .= '"' . $names . '",';*/
	}
	echo $title;

?>

 

I am wanting to set the $names field into an array where the individual array values are the names of the individual SQL table column names... anyway of doing this?

Link to comment
Share on other sites

Like this?

 

<?php

/*ini_set ("display_errors", "1");
error_reporting(E_ALL);*/

include "includes/sql.php";

$id = 24;

$query = mysql_query("SELECT * FROM added_projects WHERE id = '$id'")or die(mysql_error());

$results = mysql_fetch_object($query);

$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
while ($row = mysql_fetch_assoc($checkbox_qry)){
	foreach ($row as $k=>$val){
		echo "Column $k is $val<br>";
}
?>

 

Not sure exactly how you're wanting to echo the results.

Link to comment
Share on other sites

Then I don't know. That should ignore the first row. Look at the logic. $count is 0. The first row is grabbed, $count is still 0, it does nothing. Then, $count is incremented, so $count is no longer 0 and, therefore, each remaining row would be added to your $column variable.

 

It sounds like the row you're trying to remove is actually the second row or something.

Link to comment
Share on other sites

alrighty i will do some more digging about that one..

 

Heres another one for ya

 

I have this script here:

 

<?php $checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());

		while($row = mysql_fetch_assoc($checkbox_qry)) {
			foreach ($row as $k=>$val) {
			$column .= "Column $k is $val<br>";
			$values .= $val;
		}
	}

	if($values > 0) {
		$checked = 'checked = "checked"';
	}
$field = mysql_num_fields($checkbox_qry);

for($i = 2; $i < $field; $i++) {
$names = mysql_field_name($checkbox_qry, $i);
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																
$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $checked . ' /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>';
}
echo $title;

 

When the if($values > 0) runs it checkes all of the boxes instead of the ones with values great than 0 any ideas here?

Link to comment
Share on other sites

You want an example of what you're doing wrong? I know what you're doing wrong, but I don't know exactly what you want. Your code is kinda hard to follow. Your brackets {} appear to be off, but again, I don't know what you're trying to do.

 

The key is that you're using ONE value to assign the checked attribute, and the using it over and over. At one point, you assigned $checked to be 'checked = "checked"' and then you're using the same variable over and over and then expect it to be different in different cases.

 

See your problem?

Link to comment
Share on other sites

what I am trying to do is check the $val var and see if it equals 1 if it does then make the variable $chk = 'checked = "checked"';

 

Updated code:

 

while($row = mysql_fetch_assoc($checkbox_qry)) {
	foreach ($row as $k=>$val) {
		$column .= "Column $k is $val<br>";
		$values .= $val;
	}
}

if($values > 0) {
	$chk = 'checked = "checked"';
}

Link to comment
Share on other sites

OK, your code is adding $val to $values as a string (using the .= code), so if you had 1, 2, and 3, you would get 123, rather than 6. Then AFTER the loop, if $values (a string) is greater than 0 (a number) then they are ALL checked.

 

Are you trying to echo a checkbox for every value in the loop? If so, why isn't the echo inside the while loop?

Link to comment
Share on other sites

let me give you all of the code I am working with so you can see what I am trying to do

 

<?php
$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());

while($row = mysql_fetch_assoc($checkbox_qry)) {
	foreach ($row as $k=>$val) {
	$column .= "Column $k is $val<br>";
	$values .= $val;
}
}

if($values > 0) {
	$chk = 'checked = "checked"';
}

$field = mysql_num_fields($checkbox_qry);

for($i = 2; $i < $field; $i++) {
	$names = mysql_field_name($checkbox_qry, $i);
	$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																
	$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>';
}
	echo $title;
?>

 

I have placed the $chk var in the $title var as to say if it is greater than 0 display checked="checked"... does this help?

Link to comment
Share on other sites

<?php
if($values > 0) {
$chk = 'checked = "checked"';
}
//...
$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>';
echo $title;
?>

 

I have placed the $chk var in the $title var as to say if it is greater than 0 display checked="checked"... does this help?

 

Do you understand what I said about string vs. numeric?? Variable $values is a string, not a number. Therefore, "if ($values >0) {" doesn't make sense to PHP.

 

Also, because you're assigning $chk outside of ANY loop, it will only ever have ONE value.

 

Assuming you don't want EVERY checkbox checked, what SHOULD determine if it is checked? Is it $val being greater than zero?

 

Again I ask, why don't you do the echo in the while loop??

Link to comment
Share on other sites

ah gotcha sorry i have been working on several things today and that for some reason I was over looking :P

 

Here is the updated code:

 

$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());

$field = mysql_num_fields($checkbox_qry);

for($i = 2; $i < $field; $i++) {
	$names = mysql_field_name($checkbox_qry, $i);
	$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																
$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" /> <label for="' . $names . '">' . ucwords(str_replace($numbers, "",$names)) . '</label></div>';
}

	while($row = mysql_fetch_assoc($checkbox_qry)) {
		foreach ($row as $k=>$val) {
		$column .= "Column $k is $val<br>";
		$values .= $val;
	}
	echo $title;
}

 

And yes what determines the check is if $val > 0... how would i accomplish this?

Link to comment
Share on other sites

OK, I see you've moved some things around, and you still didn't answer my question, so I'll assume it won't be a problem to do it all in the loop. Will this work for you?

 

<?php
$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
while($row = mysql_fetch_assoc($checkbox_qry)) {
foreach ($row as $k=>$val) {
	$chk = $val>0?'checked = "checked"':"";
	echo '<div><input type="checkbox" name="checkboxes[' . $k . ']" class="checkbox" id="' . $k . '" ' . $chk . ' /> <label for="checkboxes[' . $k . '">' . ucwords($val) . '</label></div>';
}
?>

 

Again, don't know exactly what you're doing, but there ya go.

Link to comment
Share on other sites

it works like a charm :) and actually you left off a bracket.

 

Now here is one more thing!

 

The $k is displaying the mysql table column names and I need it to not display the first two... anyway of doing that with your code?

 

update code:

 

$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
	while($row = mysql_fetch_assoc($checkbox_qry)) {
		foreach ($row as $k=>$val) {
		$chk = $val>0?'checked = "checked"':"";
		$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
		echo '<div><input type="checkbox" name="checkboxes[' . $k . ']" class="checkbox" id="' . $k . '" ' . $chk . ' /> <label for="checkboxes[' . $k . '">' . ucwords(str_replace($numbers, '', $k)) . '</label></div>';
		}
	}		

Link to comment
Share on other sites

<?php
$checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
$count = 1;
while($row = mysql_fetch_assoc($checkbox_qry)) {
if ($count > 2) { // just change the 2 to however many rows you want to skip
	foreach ($row as $k=>$val) {
		$chk = $val>0?'checked = "checked"':"";
		$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
		echo '<div><input type="checkbox" name="checkboxes[' . $k . ']" class="checkbox" id="' . $k . '" ' . $chk . ' /> <label for="checkboxes[' . $k . '">' . ucwords(str_replace($numbers, '', $k)) . '</label></div>';
	}
}
$count++;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.